The advanced object-oriented learning of Python learning

Source: Internet
Author: User

First, the property attribute advanced usage can use an object method as a property Example: class foo:    def __init__ (self, age):         )         self.__age  = age     @property     def age (self):         return self.__age     @age .setter     def age (Self, args):        self.__age =  args     @age. Deleter    def age (self):         del self.__ageobj = foo print (obj.age) obj.age = 3print ( Obj.age) Del obj.ageprint (obj.age) class foo:    def __init__ (self, age) :        self.__age = age    def  Get_age (self):    &NBsp;    return self.__age    def set_age (Self, args):         self.__age = args    def  Del_age (self):         del self.__age    age  = property (Get_age, set_age, del_age) Obj = foo () print (obj.age) obj.age =  3print (obj.age) del obj.ageprint (obj.age) __str__ method, default return object memory address, can modify the override default Method Example:class foo:     def __init__ (self, name, age):         self.name = name        self.age = age     def __str__ (self):        return  ' name:  %s, age: %s '  % (self.name, self.age) obj = foo (' Egon ',  ') print (obj) _ _GETITEM__, __SETITEM__,__delitem__ can call properties and method examples by way of a dictionary: class foo:    def __init__ (self, name):         self.name = name    def __getitem __ (Self, item):         print ("GetItem")          return self.__dict__[item]    def __setitem__ (Self,  key, value):         print ("SetItem",  key, value)         self.__dict__[key] = value     Def __delitem__ (Self, key):         print (' del obj[key] , execute ')         self.__dict__.pop (key) Obj = foo (' Egon ') print ( Obj.name) #obj .name =  ' Alex ' #obj [' name '] =  ' Alex ' Print (obj[' name ']) del obj[' name '] Print (obj[' name ']) __call__To return the function, you can turn the object into a callable example: class foo:    def __call__ (Self, *args, **kwargs):         print (' ====> ') Obj = foo () obj () __getattr__ When you read a property from an object, you first need to search for the property from Self.__dicts__, and then find the property __setattr__ used to set the object from __getattr__, set the property through the __SETATTR__ function in object __ Delattr__ An example of an attribute used to delete an object: class foo:    def __init__ (self, x):         SELF.X = X    DEF __GETATTR__ (Self, item ):         print (' getattr ')     def __setattr__ (Self, key, value):        self.__dict__[key] =  value    def __delattr__ (Self, item):         self.__dict__.pop (item) Obj = foo (Obj.x = 1print) (obj.__dict__) Del obj.xprint ( obj.__dict__) OBJ.A =&NBsp;1obj.b = 2print (obj.__dict__) del obj.aprint (obj.__dict__) print ( The Obj.aaaaaaaaaexec function extracts the Python code from the string to execute an example: s= "Print (x)" g={' x ': 100000000}l={' x ': 1}exec (s,g,l) s =  "y=2 "EXEC (s, g, l) print (g) Print (L)

二、二次 Processing Standard Class

Example:

(): (itemtag=): ().        (item). Tag=tag (P_object): (p_object): (%p_object) (List). Append (P_object) (): Mid_index= ()//[mid_index] ():. Tag: () (). Clear (). Tag=l=list ([]) (L) (L.mid)
Example of implementing an iterator protocol: class foo:    def __init__ (Self, n, stop):         self.n = n        self.stop  = stop    def __next__ (self):         if self.n > self.stop:             raise StopIteration        x = self.n         self.n += 1         return x    def __iter__ (self):         return selfobj = foo (0, 5) print (Next (obj)) print (Next (obj)) print (Next (obj)) implements the context Manager: Example: class foo:    def __init__ (self, name):         self.name&nbSp;= name    def __enter__ (self):         print (' Enter ')         return self     def __exit__ (SELF,  EXC_TYPE, EXC_VAL, EXC_TB):         print (' exit ') With foo (' Alex ')  as x:    print (x)      print (' = = ')     print (' = = ')     print (' + ')


This article is from the "Linux Technology" blog, so be sure to keep this source http://haoyonghui.blog.51cto.com/4278020/1939924

The advanced object-oriented learning of Python learning

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.