Python Detailed class attributes: Class data properties, instance data properties, special class properties, property Concealment (ii) __python

Source: Internet
Author: User

Immediately following an instance of the previous class:

Class person ():
    tall = 180
    def __init__ (self,name,age,weight):
        self.name = name
        self.age =
        Age Self.weight = Weight
    def infoma (self):
        print ('%s is%s weights%s '% (self.name,self.age,self.__weight))


person = person (' Bruce ', 25,60)
Infoma = Person.infoma ()

first, data properties

1, in the above person class, "tall", "name", "Age" and "weight" are referred to as the data properties of the class, but they are divided into class data properties and instance data properties.

class Person (object):
    tall = 180
    Hobbies = []
    def __init__ (self, Name, age,weight):
        self.name = name
        Self.age = age
        self.weight = weight
    def infoma (self):
        print ('%s '%s weights%s '% (Self.name,self.age, self.weight))
person.hobbies.extend (["Football", "Woman"])   #类数据属性 belong to the class itself and can be accessed/modified by the class name, where "football", " Woman "Two
print (" Person Hobbies list:%s "%person.hobbies   ) 
# class can add class attributes after class Defi Nation
person.hobbies2 = ["reading", "Jogging", "swimming"]  #在类定义之后, class data properties can be dynamically added through class names, and new class attributes are shared by classes and all instances
print ("Person hobbies2 list:%s"%person.hobbies2  )  
print (dir)

Person Hobbies list: [' Football ', ' woman '] #类数据属性属于类本身, can be accessed/modified by class name

Person hobbies2 list: [' reading ', ' jogging ', ' swimming '] [' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ' , ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __ Subclasshook__ ', ' __weakref__ ', ' hobbies ', ' hobbies2 ', ' infoma ', ' tall '


Bruce = Person ("Bruce", 25,60)    #实例数据属性 can access print only through an instance
("%s is%d years old"% (Bruce.name, bruce.age)   )

Br Uce.gender = "Male"   #在实例生成后, you can also add instance data properties dynamically, but these instance data properties only belong to the instance
print ("%s is%s"% (Bruce.name, Bruce.gender))  

Print (dir (Bruce))
Bruce.hobbies.append ("C #")
print (bruce.hobbies)

Years old #实例数据属性只能通过实例访问Bruce the is male #实例数据属性只能通过实例访问

[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' _ _gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' _ _repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' age ', ' gender ', ' hobbies ', ' Hobbies2 ', ' infoma ', ' name ', ' tall ', ' weight ' [' Football ', ' Woman ', ' C # ']

would = person ("'ll", 27,60) 
print ("%s is%d years old"% (Will.name, will.age)) print (  

dir ())    
print (WI Ll.hobbies)

Would is 27

Years old[' __class__ ', ' __delattr__ ', ' __dict__ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __ getattribute__ ', ' __gt__ ', ' __hash__ ', ' __init__ ', ' __le__ ', ' __lt__ ', ' __module__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __weakref__ ', ' age ', ' Hobbies ', ' hobbies2 ', ' infoma ', ' name ', ' tall ', ' weight ', ' football ', ' Woman ', ' C # '

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.