The difference between Python class attributes and instance attributes

Instance attributes are common to every object and unique to each object; class attributes are the common signs of all objects

class Dog:
     "" " 
    defined dogs 
    " "" 
    Skills = [] 

    DEF  the __init__ (Self, name): 
        the self.name = name     # instance attribute is common to each object, each object-specific; all class attributes are 
        Common signs of the subject self.skills = [] 

    def add_skills (self, skill): 
        self.skills.append (skill) 

    def info (self):
         if len (self.skills)> 0:
             print ( " {} dog, Will {}! " .Format (self.name, ' - ' .join (self.skills)))
         else :
            Print ( " {} dog, nothing will! " .format (self.name)) 


# instantiated 
wangcai = Dog ( " Cai " )
 Print ( " - the dog was born - " ) 
wangcai.info () 

wangcai.add_skills ( " bark " ) 
wangcai.add_skills ( " roll " ) 
wangcai.add_skills ( " gnawing on a bone " )
 Print ( " \ n-- dogs grow up - " ) 
wangcai.info () 


taidi = Dog ( " Teddy ")
Print ( " \ n-- the dog was born - " ) 
taidi.info () 

results:
 - Dogs born - 
prosperous wealth dog, nothing will!

 - Dogs grow up - 
Wang Fortune dogs, will bark -rolling- biting bones!

 --Dogs just born-- 
Teddy dogs, nothing!

 

 

******* Please respect the original, if you want to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ , thank you! ! ******* 

Guess you like

Origin www.cnblogs.com/shouhu/p/12741587.html