|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2023/2/23 20:00
# @Author : xiongming
# @File : a-property.py
# @Desc : property 函数、类似装饰器
class A:
def __init__(self):
self._x = 250
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx)
a = A()
print(a.x)
a.x = 520
print(a.__dict__)
del a.x
print(a.__dict__)
print("------------------")
class D:
def __init__(self):
self._x = 250
def __getattr__(self, item):
if item == 'x':
return self._x
else:
super().__getattr__(item)
def __setattr__(self, key, value):
if key == 'x':
super().__setattr__('_x', value)
else:
super().__setattr__(key, value)
def __delattr__(self, item):
if item == 'x':
super().__delattr__('_x')
else:
super().__delattr__(item)
d = D()
print(d.x)
d.x = 520
print(d.__dict__)
del d.x
print(d.__dict__)
class E:
def __init__(self):
self._x = 250
@property
def x(self):
return self._x
@x.setter
def x(self):
self._x = value
@x.deleter
def x(self):
del self._x |
|