Python rookie promotion----special attribute collation

Source: Internet
Author: User

Python is all objects (object), and each object may have multiple properties (attribute).

#现在我们来总结下所有的系统定义属性和方法, first look at the reserved properties:>>> class1.__doc__       # Type help info ' Class1 doc. ' >>> class1.__name__      # type name ' Class1 ' >>> class1.__module__    # type where module ' __main__ ' >>> class1.__bases__     # The type inherits the base class (<type ' object '), >>> class1.__dict__      # Type dictionary, which stores all type member information. <dictproxy object at 0x00d3ad70> >>> Class1 (). __class__   # type <class ' __main__. Class1 ' > >>> Class1 (). __module__  # Instance type is in module ' __main__ ' >>> Class1 (). __dict__    # Object Dictionary, Stores all instance member information. {' I ': 1234}

properties of the __dict__ systemThe properties of the object may comefrom its class definition, called Class attribute. Class properties may come fromclass defines itself, may also be based onclass definition InheritanceCome on. Properties of an ObjectIt is also possible that the object instance definition, called the object property (attribute)

The properties of the object are stored in the __dict__ property of the object. __dict__ is a dictionary, the key is the property name, and the corresponding value is the property itself.

Class bird:    "" "Bird Parent Class" ""     Have_feather = True               &NBS P                          way_of_reproduction  = "egg "     song=" Twitter      def sing (self):               &NB Sp                                  Prin T (Self.song)      def move (self):          print ("Fei Fei Fei") class Chicken (Bird): & nbsp   "" "Birds sub-category" "    song=" Whoa whoa whoa!     def __init__ (self,name):        self.name=name    def move (self):   &nbs P                          ,         &NB Sp                         &NBSp                               &NBSP;&NBSP;&NBSP ;       Print ("Run and Run") Mychicken=chicken ("AQ")  print (bird.__dict__) print (chicken.__dict__) print ( MYCHICKEN.__DICT__)
Output

{' Move ': <function bird.move at 0x02fdb2b8>, ' Have_feather ': True, ' way_of_reproduction ': ' Egg ', ' __module__ ': ' __ Main__ ', ' __weakref__ ': <attribute ' __weakref__ ' of ' Bird ' Objects>, ' song ': ' Twitter ', ' __dict__ ': <attribute ' __d ict__ ' of ' Bird ' objects>, ' __doc__ ': ' Bird parent ', ' Sing ': <function bird.sing at 0x02fdb270>}                                                                                           #父类的属性 {' Move ': < function Chicken.move at 0x02fdb348>, ' __module__ ': ' __main__ ', ' __init__ ': <function chicken.__init__ at 0x02fdb300>, ' song ': ' Oh whoa! ', ' __doc__ ': ' Bird Subclass '}                 #子类的属性 {' name ': ' AQ '}                                                                                                                                                                                  #对象属性

(In the above case, we already know that the class of the summer object is chicken, and the parent class of the chicken class is bird.) If there is only one object, without knowing its class and other information, we can use the __class__ property to find the object's class, and then call the class's __base__ property to query the parent class. For example, enter print (mychicken.__class__) and the output is <class ' __main__. Chicken ' >)

PropertiesFormat 1. The first method of using attributes is now introduced:
Define three functions in the class to use as assignment, value, and delete variables (the expression may not be clear here, see example)
Class Test:def __init__ (self): Self.__x=none  def getx (self): return self.__x def setx (self,value): Self.__x=value def delx (self): del self.__x x=property (GETX,SETX,DELX, ")
The Property function prototype isProperty (Fget=none,fset=none,fdel=none,doc=none), so you can define the corresponding function according to your needs.
  
2. See the second method (new in 2.6)
  
Class C:def __init__ (self): Self.__x=none @property def x (self): return self.__x @x.setter de f x (Self,value): Self.__x=value @x.deleter def x (self): del self.__x
The three function names of the same property are the same. Called when the object. x is ready.
Role 1 by designing the property in a class class also allows the class to have arithmetic functions
2 hidden variable names, variable names and method names are not the same, the data is safe!

Class Test ():    "Property Application Example"    def __init__ (self,value):        self.value=value    def getValue (self):        Print ("Auto call value")        return self.value    def setValue (self,value):        print ("Auto perform assignment")        if value>100:            self.value=100            Print ("Value greater than 100 is assigned to")        else:            self.value=value    def delvalue (self):        del ( Self.value)        print ("delete succeeded")    Num=property (getvalue,setvalue,delvalue, ' Test ') a=test (3) print (A.num) a.num= 200print (A.value) del (a.num)
Output

Automatic call value 3 automatic execution assignment value greater than 100 is assigned to 100100 Delete succeeded



Previous: Python rookie promotion----with....as .... (Context Manager)

Next Talk:

If you have any questions, welcome to my public question ~

Python rookie promotion----special attribute collation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.