Python class Summary

Source: Internet
Author: User

Summary of Python classes
  1. Private properties

    Class Dog ():
    def Init(self):
    Self. heart=100
    D=dog ()
    D.Heart #访问不到, Heart belongs to theprivate
    Add
    two underscores to the variable before the variable becomes private, inaccessible externally, and internally accessible
    def getHeart (): return self. heart# provides access to external private properties
    Mandatory access to private properties
    D.Dog
    Heart

  2. Public properties (properties common to all Classes) class Dog () type= ' #创建公有属性d =dog () d2=dog () Dog.type #这样调用, is the same as the D.type#d,d2 property in the public variable of all class objects d2.type# When invoked with an object, a new type variable is created in the memory of its own object.
  3. Destructors
    D=dog () Del d# Delete object name to memory space reference
  4. Inherited
  5. Static methods
    Nominal collation management, in fact no property in a class or instance can be accessed in a static method
    author = ' Mr.bool '
    Class Dog (object):
    def Init(self,name):
    Self.name=name
    @staticmethod
    Def eat ():
    Print ("%s like eat Bone"%self.name)
    Print (' Dog likes to eat bones ')
    @staticmethod
    Def eat (self):
    Print ("%s like eat Bone"%self.name)
    # print (' dog likes to eat bones ')
    Def talk (self):
    Self.eat (self)
    Print (' barking ')

    D=dog ("barking")
    D.talk ()

  6. Class methods @classmethod class methods can only access class variables. Instance variables cannot be accessed. Almost rarely used
  7. Property Method @property
    Turn a method into a static property without a parenthesis call. Class Dog (): def init: Pass @property def eat (self): print (' Dog loves Bones ') @property def Eat (self,name): Print (' Dog love Eat%s '%self.name) @eat. Setter Def Eat (Self,foot): Print (' Set to foot:%s '%foot) d=dog () D.eat Call property method d.eat= ' Bun ' when the property method needs to pass parameters, it To override a method, the method name is the same as the property method name d.eat= ' Bun ' capitalization can also be used @eat when you delete an attribute method. Delete def Eat (self): del self.__food print (' deleted ')
  8. Description information for the class
    Class Dog (): "This category is a description of the dog's ' print ' (dog). Doc) Output This class is a description of the dog's
  9. module and classmodule output from which modules class output class
  10. Call D=dog () d ( ) is called after the calling object is appended with parentheses.
  11. dict View all member variables and methods in an object, use an instance call to print only properties within an instance, excluding class properties
  12. Str

    Write in class
    def str(self): return "<obj:%s>"%self.named direct Print object to output str content

  13. getitem , setitem , delitem , turn class into Dictionary
    Class Foo (object): Def Init (self): self.data={} pass def getitem (self,key): Print (' getitem ', key) return Self.data.get (key) pass Def SetItem (self,key,value): Print (' setitem ', key) Self.data[key]=value Pass def Delitem (self,key): Print (' delitem ', key) pass Obj=foo () obj[' name ']= ' Alex ' #自动触发执行 getitem print (obj[' name ') #自动触发执行 setitem del obj[' K1 ' ] #自动触发执行 Delitem
  14. New Metaclass class is also an object, type is class of Class
    Class Dog (): Passtype (Dog) will know that class is of type The new method executes new inside the Init constructor method, calling Init
    def new (CLS, args, *kwargs):p rint (' Foo--new--') return object. New (CLS) when return is commented, Init is not called, which inherits the parent class's new method Metaclass Primitive class

    Class MyType (): def init (self): print ("MyType init") 1 pass def call (self): print ("MyType Call ") 2class Dog (): metaclass =mytype def init (self): print (" Dog init ") 4 pass def new (self): print ("Dog new") 3
    Call Create new new Create init

  15. Reflection
    GetAttr setattr hasattr delattrclass Foo (object):

    def __init__(self):
    self.name = ‘wj‘
    def func(self):
    return ‘func‘

    obj = Foo ()

    Check if the member # # # #hasattr (obj, ' name ') hasattr (obj, ' func ') is included

    Get member # # # #getattr (obj, ' name ') getattr (obj, ' func ')

    Set member # # # #setattr (obj, ' age ', setattr) (obj, ' show ', Lambda Num:num + 1)

    Delete Member # # # #delattr (obj, ' name ') delattr (obj, ' func ')

Python class Summary

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.