Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Time : 2018/11/14 18:52
- # Author : Jenson
- class A():
- def __init__(self):
- self.name = "Jenson"
- self.age = "18"
- def fget(self): #此功能是对类变量读取时要执行的函数功能
- print("我被读取了")
- return self.name
- def fset(self, name):
- print("我被写入了,但还是可以做好多事情")
- self.name = "怡然科技:" + name
- def fdel(self):
- pass
- name2 = property(fget, fset, fdel, "This is the first time I use property function!")
- #增加一个成员变量name2,property函数的四个参数顺序固定的
- #第一个参数fget是读取时要调用的函数(fget是一个函数,此时在property中作为一个参数)
- #第二个参数fset是写入时要调用的函数(fset是一个函数,此时在property中作为一个参数)
- #第三个参数fdel是删除时要调用的函数(fdel是一个函数,此时在property中作为一个参数)
- a = A() #实例化一个a,但是对象a现在是空的,啥也没有
- print(a)
- a.name = "Annie" #类属性赋值(写操作),如果说赋值时有附加操作,可以通过property实现
- print(a.name) #类属性读取
- del a.name #类属性删除
- print(a.name2) #读取操作,property会自动调用fget函数
Add Comment
Please, Sign In to add comment