我试图在我的课程附件上设置属性name
。 name属性需要根据它是否是zip文件来设置。如果是zip文件,我需要返回解压缩的文件名而不是zip文件名。这是班级:
from os.path import splitext
class Attachment(object):
def __init__(self, name):
self.__name = name
if self.__name.endswith(".zip"):
self.zip_contents = {"content":"test", "name":"testing.txt"}
@property
def extension(self):
_, ext = splitext(self.__name)
return ext.lower()
@property
def name(self):
print('Called getter')
return self.__name
@name.setter
def name(self, name):
print('Calling setter')
if name.endswith(".zip"):
self.__name = self.zip_contents["name"]
else:
self.__name = name
@name.deleter
def name(self):
del self.__name
test = Attachment("testing.zip")
print test.name
我在尝试打印test.name
Called getter
testing.zip
有人可以解释我在这里做错了什么以及为什么没有调用setter?谢谢!