问题描述
因此,我尝试为class属性创建特定的字符串表示形式。该属性是一个整数标志,以后将采用0到2的值,我想基于标志的值进行字符串表示。
应该给出答案但实际上不起作用的问题是@L_502_0@。有用的属性问题的解释是here。
代码:
class MyProperty(property):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
def __str__(self):
return "flag is equal to 0"
# Eventually I need a more complex behavIoUr that depends on flag value like this:
# return f"{self.__name__} is equal to {self.__???__}\n"
# but I even don't kNow how to do it in the most simple case.
class Agent:
def __init__(self):
self.flag = 0
@MyProperty
def flag(self):
return self._flag
@flag.setter
def split_status(self,value):
self._flag = value
@flag.deleter
def deleter(self):
del self._flag
ag = Agent()
print(ag.flag)
所需的输出:
flag is equal to 0
输出:
Traceback (most recent call last):
File "C:/Users/admin/.../prop.py",line 27,in <module>
ag = Agent()
File "C:/Users/admin/.../prop.py",line 12,in __init__
self.flag = 0
AttributeError: can't set attribute
感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)