Quick actions

cmd+k|ctrl+k

Navigation

Languages

Python Web-L08(class)

Snippet info

Language

Python

Visibility

public

Author

victormakwaitat

Created

2025-02-13T02:11:18.415053Z

Updated

2025-02-13T04:41:09.135232Z

class Toy:
    head = 10
    body = 20
    def __init__(self, x=1, y=2): 
        self.head1 = x
        self.body1 = y
    def run(self): # self points at obj; self is dummy variable;self is simple variable
        print("toy runs", self.head,self.head1)
    def run2(self, x, y):
        print("toy runs", x, y)
    def obj_name(self):
        print(self.__str__())
    def obj_name2(self):
        self.__str__ = self.head
        print(self.__str__)
toy1 = Toy()
toy2 = Toy(0,0)
toy3 = Toy(1,2)
toy4 = Toy(3,4)
print(toy1, toy2)
print(toy1.head,toy2.body,toy1.run()) # Result = 10,20,None
toy1.run()
print(Toy.__dict__)
print(toy3.head1, toy3.body1, toy4.head1, toy4.body1)
toy3.run2(12,13)

def b():
    print("Hello")
toy1.say_hello = b
print(toy1.say_hello.__defaults__)
print(toy1.say_hello.__class__)
print(toy1.say_hello.__name__)
toy1.say_hello()
toy1.obj_name()
toy1.obj_name2()
print(dir(toy1)) # Original
del toy1.say_hello
del toy1.head1
print(dir(toy1)) # latest

import inspect
print(inspect.getmembers(toy1)) # inspect.getmembers() <- get more details of class; we can see the content of class object
INFO