python核心-面向对向-属性相关

 

 
# 1.定义一个类
class Person:
    pass

# 2.根据类创建一个对象
# p1 = Person()
# p2 = Person()
# p1.age = 18
# p2.address = "上海"
# print(p1.address)  #'Person' object has no attribute 'address'

# 3.给p对向,增加一些属性
# p.age = 18
# print(id(p.age))
# p.hight = 180
# p.age = 123
# print(id(p.age))
# # 4.验证是否添加成功
# print(p.age)
# # print(p.__dict__)

# print(p.sex) # 'Person' object has no attribute 'sex'

# p.pets = ['xiaohua', 'xiaohei']
# print(id(p.pets))
# p.pets.append("xiaohong")
# print(id(p.pets))
# p.pets = ['1', '2']
# print(id(p.pets))

# p.age = 18
# del p.age
# print(p.age)

 

 


# class Money:
#     pass
# # one = Money()
# # one.age = 18
# Money.count = 1
# Money.age = 18
#
# print(Money.count)
# print(Money.__dict__)

# class Test:
#     sex = "男"

class Money:
    age = 18
    count = 1
    num = 666

# Money.age = 22
# print(Money.age) # 属性存在就是修改,不存在就是增加


one = Money()

# del  Money.age  # AttributeError: type object 'Money' has no attribute 'age'
del one.age # AttributeError: age
# print(Money.age)
print(one.age)



# print(Money.age)
#
# one.age = 19   # 不能通过对向修改
# print(one.age)

# print(Money.count)
# print(Money.age)
# print(Money.num)
# print(Money.__dict__)

# one = Money()
# one.__class__ = Test
# one.sex = "女" # 优先从自己身上找
# print(one.__class__)
#
# print(one.sex)
# print(one.age)
# print(one.count)
# print(one.num)

 

class Money:
    age = 18
    name = "sz"
# Money.__dict__ = {"sex": "男"}  # AttributeError: attribute '__dict__' of 'type' objects is not writable
print(Money.__dict__)
# Money.__dict__["age"] = 20 # TypeError: 'mappingproxy' object does not support item assignment



one = Money()

one.age = 18
one.height = 180
#
# print(one.__dict__)
# print(Money.__dict__)

# one.__dict__ = {"name": "sz", "age": 18}
one.__dict__["age"] = 999
print(one.name)

 


# class Person:
#     age = 10
#     pass


# Person.age = 19
# Person.age = 11
# del Person.age
# Person.age

# p = Person()
# p.age += 5
#
# print(Person.age)
# print(p.age)



class Person():
    __slots__ = ["age"]
    pass

p1 = Person()
p1.age = 1
p2.age = 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值