0

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!

staten12
  • 735
  • 3
  • 9
  • 20
  • i run your code and it works .., can you give all traceback ?, and change `self._name` in `extension with `self.__name` – Druta Ruslan Jun 07 '18 at 14:08
  • Is this Python 2.7 or 3-x? It cannot be both. Also please fix indentation in init method. Once that is done, your code works for me – Olivier Melançon Jun 07 '18 at 14:08
  • I fixed the indent errors, and still am only getting this during print `Called getter testing.zip` The results should be printing `testing.txt` so it is not calling the setter method – staten12 Jun 07 '18 at 14:22

0 Answers0