Methods for object-oriented private properties (plus two underscores) the difference between modern class classic classes

Source: Internet
Author: User

@classmetod

#!/usr/bin/env python#-*-coding:utf-8-*-class Animal:    def __init__ (self,name):        self.name = name    Hobbie = The ' meat '    @classmethod   #类方法不能访问实例变量  plus  self.name can't access def Talk    (self):        #print ("%s is Talking "%self.name)        print ("%s is Talking "%self.hobbie) Animal.hobbieAnimal.talk () d = Animal (" Wangwang ") D.talk ()

  

@staticmethon

#!/usr/bin/env python#-*-coding:utf-8-*-class Animal:    def __init__ (self,name):        self.name = name    Hobbie = ' Meat '    @classmethod   #类方法不能访问实例变量  Self.name will not be able to access    def talk (self):        #print ("%s is Talking "%self.name"        print ("%s is Talking"%self.hobbie) "    @staticmethod   #静态方法, it has nothing to do with the case, instance variables and class variables are inaccessible,    def Walk ():        print ("is walking ...") #Animal. Hobbie#animal.talk () d = Animal ("Wangwang") D.walk ()

  

@property

#!/usr/bin/env python#-*-coding:utf-8-*-class animal:def __init__ (self,name): Self.name = name SELF.N Um = None Hobbie = ' meat ' @classmethod #类方法不能访问实例变量 self.name cannot access def talk (self): #print ("%s I S talking "%self.name) print ("%s is Talking "%self.hobbie) @staticmethod #静态方法 with no relation to the case, instance variables and class variables are inaccessible, D    EF Walk (): Print ("is walking ...") @property #加了property后babit就不是方法了, when used without parentheses, is a property. The class variable instance variable can also be accessed by turning the method into a property. The value cannot be passed. If you do not want to pass the value to see the following example, delete also under Def habit (self): print ("[%s] habit is Xxoo"%self.name) #往里面传值 @property def total_p        Layers (self): return self.num @total_players. Setter def total_players (self,num): Self.num = num Print ("Total players:", Self.num) #删值 @total_players. Deleter def total_players (self): print ("Total player Got Delted ") del Self.num#animal.hobbie#animal.talk () # d = Animal (" Wangwang ") # D.walk () d = Animal (" Wangwang ") D.habi Tprint (D.total_players) d.total_players = 3del D.total_playersprint (d.total_players) 

Private variable (private property)) is not accessible outside, if special circumstances are not to access it is also possible

Print ("Out__num:", d.__num) changed to print ("Out__num:", D._animal__num)

#!/usr/bin/env python#-*-coding:utf-8-*-class animal:def __init__ (self,name): Self.name = name Self._  _num = None Hobbie = ' meat ' @classmethod #类方法不能访问实例变量 self.name cannot access def talk (self): #print ("%s    Is talking '%self.name ' Print ("%s is Talking"%self.hobbie) @staticmethod #静态方法, has nothing to do with the case, the instance variables and class variables are inaccessible,    Def walk (): Print ("is walking ...") @property after #加了property Babit is not a method, it is a property. The class variable instance variable can also be accessed by turning the method into a property. The value cannot be passed. If you do not want to pass the value to see the following example, delete also under Def habit (self): print ("[%s] habit is Xxoo"%self.name) #往里面传值 @property def total_p        Layers (self): return self.__num @total_players. Setter def total_players (self,num): Self.__num = num Print ("Total players:", Self.__num) #删值 the @total_players. Deleter def total_players (self): print ("Total p Layer got delted ") del Self.num#animal.hobbie#animal.talk () # d = Animal (" Wangwang ") # D.walk () d = Animal (" Wangwang ") D.habitprint (D.total_players) d.total_players = 3d.__num = 9print ("Out__num:", D.__num) #del d.total_playersprint (d.total_players) 

  

The difference between inheriting a classic class and a new class

#!/usr/bin/env python#-*-coding:utf-8-*-# B > C > A breadth First  from left to right from the same level to find, and then go to a   new class # B > A > C                   Depth First   Classic Class # The difference between the new class and the classic class is the difference between py2 and Py3, the same code with 2.7 is the depth first class A:                               #经典类 #class A (object):                       #新式类    n = "a"    def F2 ( Self):        print ("AF2") class B (A):    n = "B"    def F1 (self):        print ("B")    def F2 (self):        print ("B2 ") class C (A):    n =" C "    def F2 (self):        print (" C ") class D (b,c):    PASSD = d () #d. F1 () d.f2 () #面向对象特性之多态" __DOC__ Display class explanation annotation __del__ destruction method and so on after the execution of the unified destroy __call__ instantiation Plus () automatically execute the  call method without an egg __new__ the class rewrite, new calls Init   temporarily useless __dict_ The member variables of the _ class are displayed in the dictionary form.

  

Methods for object-oriented private properties (plus two underscores) the difference between modern class classic classes

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.