1. Static fields and Normal fields
Class Foo: #静态字段, saved in class memory cc= ' 1234 ' def __init__ (self): #动态字段 (normal field), saved in object self.name = ' Alex ' def Show (self): print (Self.name) "If the normal field is a fixed value, like country = ' China ', each class will have a memory waste, all will have a fixed worth normal field to static field static field is created when the code is loaded, ordinary fields create an instance when the "Class Province (): def __init__ (self , name): self.name = name country = ' China ' hn= province (' henan ') HB = Province (' Hebei ') ' Normal fields can only be accessed using the Object access static field (accessed with the object as a last resort) "Class province (): country = ' China ' #普通方法, called by the object to execute (method belongs to Class) def _ _init__ (self,name): self.name = namehn= province (' Henan ') print (province.country) print (hn.country)
2. Static method, class method
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' common method: at least one self Like execute static method: arbitrary parameter, no Self class execution (object can also execute) class method: At least one CLS class execution (object can also execute) "" ' Static Method ' class Province (): Country = ' China ' "' common method, called by object to execute (method belongs to Class) ' Def __init__ (self, name): Self.name = name def show (self): print (Self.nam e) A static method, called by a class, similar to a static field, can call a function equivalent to Python when the object is not created, because it is written in a class because it is related to a class, and if it has nothing to do with the class, you can write a function "' @staticme Thod def F1 (ARG1,ARG2): Print (ARG1,ARG2) "class method, must have one parameter is CLS, class method is a special case of static method, and static method compared to the class method is automatically passed to class method a class name , and the static method does not pass the parameter CLS: the class name of this class "@classmethod def F2 (CLS): Print (CLS) province.f1 (' A ', ' B ') class province (): def __init__ (self,all_count): Self.all_count = All_count ' property: Do not add when calling () at this time, can only take value, cannot pass value, if want to pass a value, want to write a name again The same method is then added with an adorner, @ function name. Setter If you want to delete, add an adorner @ function name. deleter synthesis can be used in this form: Def F1 (): Pass def F2 (): Pass Def f3 (): Pass Foo = property (Fget = f1, Fset = F2, Fdel = F3) outside the class, so called: P.foo ==> Fget=all_pager () P.foo = ' ==> Fset=all_pager () del p.foo ==> Fdel=all_pager () ' @property def All_pager (self): Print (Self.all_count) @all_pager. Setter def all_pager (self,value): print (value) @all_pager. deleter def all_pager (self): print (' del all_pager ') p = province (+) # p.all_count () P.all_pagerp.all_pager = 10del P.all_ Pager #关联方式, this code will execute
3. Private fields
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=class foo: ' private static field As with private fields, access can only be made within the class ' __cc = ' 123 ' def __init__ (self,name,age): ' total field ' ' self.name=name ' private fields, which can only be used within this class, cannot be inherited or ' self__age = ageobj = foo (' name ', ' * ') print (obj.name) ' Print (obj.age) because age is a private field, All exceptions when accessed outside the class, Attributeerror: ' Foo ' object has no attribute ' age '
10. Class