There are two types of members of a class
Public members, accessible from anywhere
The first two characters of a private member are underlined only if the method is internal to the class and the private member is named.
class Foo: def __init__(self, name, age): self.name = name self.__age = age def show(self): # 间接方法私有字段 return self.__age obj = Foo(‘klvchen‘, 25)print(obj.name)res = obj.show()print(res)运行结果:klvchen25
public static fields: classes are accessible, inside classes can be accessed, and in derived classes
private static field: Only the class can be accessed internally;
class Foo: __v = ‘666‘ # 私有静态字段 def __init__(self): pass def show(self): return Foo.__vobj = Foo()res = obj.show()print(res)运行结果:666
class Foo: __v = ‘666‘ def __init__(self): pass def show(self): return Foo.__v @staticmethod def stat(): return Foo.__vres = Foo.stat()print(res)运行结果:666
Cannot inherit private field from parent class
class F: def __init__(self): self.ge = 123 self.__gene = 456 #私有字段class S(F): def __init__(self, name): self.name = name self.__age = 18 super(S, self).__init__() def show(self): print(self.name) print(self.__age) print(self.ge) print(self.__gene)s = S(‘klvchen‘)s.show()运行结果:klvchen18123AttributeError: ‘S‘ object has no attribute ‘_S__gene‘
A special member of the class, int (object), automatically executes the __int__ method in the object, assigns the return to the Int object, and, in the same way Str (object), automatically executes the __str__ method and returns an assignment to the Str object.
class Foo: def __init__(self): pass def __int__(self): return 666 def __str__(self): return ‘hello world‘obj = Foo()print(obj, type(obj))res = int(obj)print(res)res1 = str(obj)print(res1)运行结果:<__main__.Foo object at 0x0000022BBE9DA978> <class ‘__main__.Foo‘>666hello world
Print (object), str (object), automatically executes the __str__ method in the object, and returns
class Foo: def __init__(self, n, a): self.name = n self.age = a def __str__(self): return ‘%s-%d‘ %(self.name, self.age)obj = Foo(‘klvchen‘, 28)print(obj)运行结果:klvchen-28
When two objects are added, the __add__ method of the first object is executed automatically, and the second object is passed in as a parameter
class Foo: def __init__(self, name, age): self.name = name self.age = age def __add__(self, other): return self.age + other.ageobj1 = Foo(‘klv1‘, 23)obj2 = Foo(‘klv2‘, 24)res = obj1 + obj2print(res, type(res))运行结果:47 <class ‘int‘>
class Foo: def __init__(self, name, age): self.name = name self.age = age def __add__(self, other): return Foo(obj1.name, obj2.age) def __del__(self): print(‘析构方法‘)obj1 = Foo(‘klv1‘, 23)obj2 = Foo(‘klv2‘, 24)res = obj1 + obj2print(res, type(res))运行结果:<__main__.Foo object at 0x0000016DFCE125C0> <class ‘__main__.Foo‘>析构方法析构方法析构方法
The Li[object] automatically executes the __getitem__ method in the class of the Li object, and 8 is passed as a parameter to the item
class Foo: def __init__(self, name, age): self.name = name self.age = age def __getitem__(self, item): return itemli = Foo(‘klvchen‘, 28)r = li[8]print(r)运行结果:8
The __setitem__,__delitem__ method in a class
class Foo: def __init__(self, name, age): self.name = name self.age = age def __getitem__(self, item): return item def __setitem__(self, key, value): print(key, value) def __delitem__(self, key): print(key)li = Foo(‘klvchen‘, 28)r = li[8]print(r)li[100] = ‘hello‘del li[999]运行结果:8100 hello999
Executes the __iter__ () method in the class and gets its return value, looping back the object in the previous step, for iterators, the reason that the list, dictionary, and tuple can be used for loops because the type internally defines the
ITER
class Foo: def __init__(self, name, age): self.name = name self.age = age def __iter__(self): return iter([11, 22, 33])li = Foo(‘klvchen‘, 26)for i in li: print(i)运行结果:112233
Internal operation for the For loop
obj = iter([11, 22, 33])while True: val = obj.next() print val
Special members of the Python class are introduced