Class has some special attributes, so that we can obtain some additional information.
1 >>> class class1 (object): 2 "class1 Doc. "3 def _ init _ (Self): 4 self. I = 1234 5 6 >>> class1. _ Doc _ # type help information 7' class1 Doc. '8 >>> class1. _ name _ # type name 9 'class1' 10 >>>> class1. _ module _ # type module 11' _ main __' 12 >>> class1. _ bases _ # base class 13 inherited by the type (<type 'object'> ,) 14 >>> class1. _ dict _ # type dictionary, which stores information about all types of members. 15 <dictproxy object at 0x00d3ad70> 16> class1 (). _ class _ # type 17 <class '_ main __. class1 '> 18 >>> class1 (). _ module _ # Module 19 '_ main _' 20> class1 (). _ dict _ # object dictionary, which stores information about all instance members. 21 {'I': 1234}
Member
Python class also contains two types of members: type and instance.
1 >>> class Class1:2 i = 123 # Class Field3 def __init__(self):4 self.i = 12345 # Instance Field5 6 >>> print Class1.i7 1238 >>> print Class1().i9 12345
Two important functions for retrieving instance attributes:
Getattr (instance, attribute name) and hasattr (instance, attribute name) Determine whether the attribute name belongs to the instance.
For example, S = 'I, getattr (class1 (), S) = 12345
Hasattr (class1 (), S) = true
-----------------------
There are several "special" rules "that need attention.
(1) We can reference access type members through instances. Therefore, in the following example, self. I actually points to class1. I until we add a member I to the instance.
1> class class1: 2 I = 123 3 def _ init _ (Self): 4 print self. I 5 print hex (ID (self. i) 6 7 >>> hex (ID (class1. I) # display class1. I 8 '0xab57a0 '9 >>> A = class1 () # create a class1 instance, we will find that self. I actually points to class1. I. 10 12311 0xab57a012 >>> class1. _ dict _ # Show the class1 member 13 {'I': 123, '_ module _': '_ main __', '_ Doc _': None, '_ init _': <function _ init _ at 0x00d39470 >}14 >>>. _ dict _ # display instance Member 15 {} 16 >>>. I = 123456789 # Add a member i17> hex (ID (. i) # display the new instance Member address 18 '0xbbb674' 19 >>>. _ dict _ # display instance Member 20 {'I': 123456789}
You can add "_" before the member name to make it a private member.
1 >>> class Class1:2 __i = 1233 def __init__(self):4 self.__x = 05 def __test(self):6 print id(self)
In fact, this is only a rule, not a limitation on the compiler. We can still use special syntax to access private members.
1 >>> Class1._Class1__i2 1233 >>> a = Class1()4 >>> a._Class1__x5 06 >>> a._Class1__test()7 13860376
-----------------------
In addition to static (type) fields, we can also define static methods.
1 >>> class Class1:2 @staticmethod3 def test():4 print "static method"5 >>> Class1.test()6 static method
Heavy Load
Python supports some special methods and operator overloading.
1 >>> class Class1: 2 def __init__(self): 3 self.i = 0 4 def __str__(self): 5 return "id=%i" % id(self) 6 def __add__(self, other): 7 return self.i + other.i 8 9 >>> a = Class1()10 >>> a.i = 1011 >>> str(a)12 'id=13876120'13 >>> b = Class1()14 >>> b.i = 2015 >>> a + b16 30
By reloading "_ eq _", we can change the behavior of the "=" operator.
1 >>> class Class1: 2 pass 3 4 >>> a = Class1() 5 >>> b = Class1() 6 >>> a == b 7 False 8 9 >>> class Class1:10 def __eq__(self, x):11 return 5512 >>> a = Class1()13 >>> b = Class1()14 >>> a == b15 55
[Transfer] http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035690.html