[python] Object-oriented notes

1. Inherit
super()
multiple inheritance approximate breadth first C3 algorithm

2. Private attributes and private methods.
Add two underscores before age and breath of self.age, def breath, they cannot be accessed from outside, they can be accessed in the class instance

3. Access to private methods and attributes
a._Dog__sh() #Dog is the class name

4. Throw an exception
raise the exception type ("content") # can remind the subclass to override the parent class method

5. Class method
@classmethod decoration, turn self itself into the class itself.
You can't access instance variables , only class variables (the ones without self at the time of definition, non-instance, and the variables belonging to the class written outside def)

6, class attribute modification
Insert picture description here
7, static method
@staticmethod decoration, can not access class variables, nor can access instance variables, cut off any relationship between it and the class and instance; nominally only placed under the class; the function input variable self becomes Input variables, no longer refer to instances

8. The property method property
turns a method into a static property. For example, the execution method does not need to add (self). The
calling method is a variable, but the outside world cannot modify it;
Insert picture description here
if you still want to modify it, the method name is .setter
Insert picture description here
9. Reflection
hasattr(obj,name) #Use string to determine whether the object has the attribute or method
getattr(obj,name) #get
setattr(obj,name,new)
#assignment delattr #delete

class Dog:
    def walk(self):
        print("我硕化")

dog = Dog()

youwantofind = input(">>").strip()
if hasattr(dog,youwantofind):
    func = getattr(dog,youwantofind)
    print(func())

setattr

def sh(self):
	print("111")
dog = Dog()
setattr(dog,"sh",sh)#若是dog换成Dog,则下一行不需要传入实例
dog.sh(dog)#注意这里,有点像静态方法staticmethod

10. The magic of reflection # Take the menu interface as an example.
Insert picture description here
Think about the process-oriented editing...


11. Double underline method
(1) len
will trigger as long as len is an object

(2) The hash method is the same as
(3) Triggered when eq
judges a1==a2

(4) Item series
As long as the corresponding execution is executed, it will be triggered.
Insert picture description here
12. Str & repr
Insert picture description here
13. The destructor method will be executed when the
del
Insert picture description here
program is released.

14. The __new__ method is executed before __init__
# to achieve singletonization
Insert picture description here
Insert picture description here
15. Call
instance name + parentheses or class name + parentheses, such as s=School(), s() or School()() Time to execute

16. Dynamically create a class type
Insert picture description here
17, isinstance(0bj,cls) to
determine whether obj is of this type

18. assert assertion
Insert picture description here
19. module__ and __class
view which module and which class the current operation object is in

Guess you like

Origin blog.csdn.net/Sgmple/article/details/111499867