【python】详解类class的属性:类数据属性、实例数据属性、特殊的类属性、属性隐藏(二)

本文详细探讨了Python中类的属性概念,包括类数据属性与实例数据属性的区别、特殊类属性的作用及属性隐藏现象,通过具体示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自上一篇python中的类,对象,方法,属性初认识(一)认识了类的基本架构,下面继续对类进行详解,更加深入了解类的属性、方法、访问控制这三个方面的类容。

紧接上一篇类的实例:

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()

一、数据属性

1、在上面的person类中,“tall”、“name”、"age"和"weight "都被称为类的数据属性,但是它们又分为类数据属性和实例数据属性。

首先看一段代码,代码中分别展示了对类数据属性和实例数据属性的访问:

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 is %s weights %s'%(self.name,self.age,self.weight))
person.hobbies.extend(["football", "woman"])   #类数据属性属于类本身,可以通过类名进行访问/修改,此处添加"football"、"woman"两个
print("person hobbies list: %s" %person.hobbies   ) 
# class can add class attribute after class defination
person.hobbies2 = ["reading", "jogging", "swimming"]  #在类定义之后,可以通过类名动态添加类数据属性,新增的类属性也被类和所有实例共有
print( "person hobbies2 list: %s" %person.hobbies2  )  
print( dir(person))


Bruce = person("Bruce", 25,60)    #实例数据属性只能通过实例访问
print ("%s is %d years old" %(Bruce.name, Bruce.age)   )
# class instance can add new attribute 
# "gender" is the instance attribute only belongs to wilber
Bruce.gender = "male"   #在实例生成后,还可以动态添加实例数据属性,但是这些实例数据属性只属于该实例
print( "%s is %s" %(Bruce.name, Bruce.gender) )  
# class instance can access class attribute
print( dir(Bruce))
Bruce.hobbies.append("C#")
print (Bruce.hobbies)


will = person("Will", 27,60) 
print( "%s is %d years old" %(will.name, will.age) )  
# will shares the same class attribute with wilber
# will don't have the "gender" attribute that belongs to wilber
print( dir(will) )    
print (will.hobbies)

通过内建函数dir(),或者访问类的字典属性’_ _dict _ _'这两种方式都可以查看类有哪些属性,代码的输出为:

person hobbies list: ['football', 'woman']   #类数据属性属于类本身,可以通过类名进行访问/修改
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 is 25 years old  #实例数据属性只能通过实例访问
Bruce 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#']
Will 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#']

对于类数据属性和实例数据属性,可以总结为:

  • 类数据属性属于类本身,可以通过类名进行访问/修改
  • 类数据属性也可以被类的所有实例访问/修改
  • 在类定义之后,可以通过类名动态添加类数据属性,新增的类属性也被类和所有实例共有
  • 实例数据属性只能通过实例访问
  • 在实例生成后,还可以动态添加实例数据属性,但是这些实例数据属性只属于该实例

2、特殊的类属性:对于所有的类,都有一组特殊的属性

  • _ _ name_ _:类的名字(字符串)
  • _ _ doc _ _ :类的文档字符串
  • _ _ bases _ _:类的所有父类组成的元组
  • _ _ dict _ _:类的属性组成的字典
  • _ _ module _ _:类所属的模块
  • _ _ class _ _:类对象的类型
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 is %s weights %s'%(self.name,self.age,self.weight))

print(person.__name__)
print(person.__doc__)
print(person.__bases__)
print(person.__dir__)
print(person.__module__)
print(person.__class__)

输出结果为:

person
None
(<class 'object'>,)
<method '__dir__' of 'object' objects>
__main__
<class 'type'>

3、属性隐藏:类数据属性属于类本身,被所有该类的实例共享;并且,通过实例可以去访问/修改类属性。但是,在通过实例中访问类属性的时候一定要谨慎,因为可能出现属性"隐藏"的情况。

Bruce = person("Bruce", 25,180)

print("person.tall is Bruce.tall: ", person.tall is Bruce.tall)
Bruce.tall = 185    #重新赋值或者修改
print( "person.tall is Bruce.tall: ", person.tall is Bruce.tall)
print( person.__dict__)
print( Bruce.__dict__)
del Bruce.tall   #再次删除实例的赋值
print("person.tall is Bruce.tall: ", person.tall is Bruce.tall)  #person.tall is Bruce.tall为True


Bruce.tall += 3    
print("person.tall is Bruce.tall: ", person.tall is Bruce.tall)
print(person.__dict__)
print(Bruce.__dict__)


del Bruce.tall

print("person.hobbies is Bruce.hobbies: ", person.hobbies is Bruce.hobbies)
Bruce.hobbies = ["C#", "Python"]
print ("person.hobbies is Bruce.hobbies : ", person.hobbies is Bruce.hobbies)
print( person.__dict__)
print( Bruce.__dict__)
del Bruce.hobbies
print( "person.hobbies is Bruce.hobbies: ", person.hobbies is Bruce.hobbies)

print() 

Bruce.hobbies.append("CSS")
print("person.hobbies is Bruce.hobbies: ", person.hobbies is Bruce.hobbies)
print(person.__dict__)
print(Bruce.__dict__)

输出结果:

person.tall is Bruce.tall:  True
person.tall is Bruce.tall:  False
{'tall': 180, 'infoma': <function person.infoma at 0x000001C9B9D0D6A8>, '__dict__': <attribute '__dict__' of 'person' objects>, '__module__': '__main__', '__init__': <function person.__init__ at 0x000001C9B9D0DE18>, '__weakref__': <attribute '__weakref__' of 'person' objects>, 'hobbies': [], '__doc__': None}
{'weight': 180, 'name': 'Bruce', 'tall': 185, 'age': 25}
person.tall is Bruce.tall:  True
person.tall is Bruce.tall:  False
{'tall': 180, 'infoma': <function person.infoma at 0x000001C9B9D0D6A8>, '__dict__': <attribute '__dict__' of 'person' objects>, '__module__': '__main__', '__init__': <function person.__init__ at 0x000001C9B9D0DE18>, '__weakref__': <attribute '__weakref__' of 'person' objects>, 'hobbies': [], '__doc__': None}
{'weight': 180, 'name': 'Bruce', 'tall': 183, 'age': 25}
person.hobbies is Bruce.hobbies:  True

person.hobbies is Bruce.hobbies:  False
{'tall': 180, 'infoma': <function person.infoma at 0x000001C9B9D0D6A8>, '__dict__': <attribute '__dict__' of 'person' objects>, '__module__': '__main__', '__init__': <function person.__init__ at 0x000001C9B9D0DE18>, '__weakref__': <attribute '__weakref__' of 'person' objects>, 'hobbies': [], '__doc__': None}
{'weight': 180, 'name': 'Bruce', 'hobbies': ['C#', 'Python'], 'age': 25}
person.hobbies is Bruce.hobbies :  True

person.hobbies is Bruce.hobbies:  True
{'tall': 180, 'infoma': <function person.infoma at 0x000001C9B9D0D6A8>, '__dict__': <attribute '__dict__' of 'person' objects>, '__module__': '__main__', '__init__': <function person.__init__ at 0x000001C9B9D0DE18>, '__weakref__': <attribute '__weakref__' of 'person' objects>, 'hobbies': ['CSS'], '__doc__': None}
{'weight': 180, 'name': 'Bruce', 'age': 25}

对于不可变类型的类属性,隐藏属性可以总结为:

  • 对于不可变类型的类属性person.tall,可以通过实例Bruce进行访问,并且"person.tall is Bruce.tall"

  • 当通过实例赋值/修改tall属性的时候将为实例Bruce新建一个tall实例属性,这时,“person.tall is not Bruce.tall”

  • 当通过"del Bruce.tall"语句删除实例的tall属性后,再次成为"person.tall is Bruce.tall"

对于可变类型的类属性,隐藏属性可以总结为:

  • 同样对于可变类型的类属性person.hobbies,可以通过实例Bruce进行访问,并且"person.hobbies is
    Bruce. hobbies"
  • 当通过实例赋值hobbies 属性的时候,都将为实例Bruce新建一个hobbies实例属性,这时,“person.hobbies is not
    Bruce. hobbies”
  • 当通过"del Bruce. hobbies"语句删除实例的hobbies属性后,再次成为"person. hobbies is Bruce.
    hobbies"
  • 当通过实例修改hobbies属性的时候,将修改Bruce. hobbies指向的内存地址(即person.hobbies),此时,“person.hobbies is not Bruce. hobbies”

注意,虽然通过实例可以访问类属性,但是,不建议这么做,最好还是通过类名来访问类属性,从而避免属性隐藏带来的不必要麻烦

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值