I'm trying to set an attribute name
on my class Attachment. The name attribute needs to be set based on whether it's a zip file or not. If it is a zip file I need to return the unzipped filename rather than the zip filename. Here is the class:
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
I am receiving the following when I try printing test.name
Called getter
testing.zip
Can someone explain what I am doing wrong here and why the setter is not being called? Thanks!