0

I'm programming a script with Python using instances of FileHandler class but the second overwrites the first even without being assigned to the same variables.

The class:

class FileHandler():

    name = None
    path = None

    @classmethod 
    def __init__(self,name,path):
        self.name=name
        self.path=path

    @classmethod
    def getName(self):
        return self.name

    @classmethod
    def getPath(self):
        return self.path

The script:

import fileHandler 

origen=fileHandler.FileHandler('a','b')
destino=fileHandler.FileHandler('c','d')

print origen.getName(),origen.getPath()
print destino.getName(),destino.getPath()

The result:

c d
c d
martineau
  • 119,623
  • 25
  • 170
  • 301
Marco
  • 670
  • 5
  • 16
  • 4
    because you made them class methods, the self there is really the class. all instances then share these values. just use regular methods – pvg Feb 17 '17 at 18:38
  • 3
    @Marco What do you think `@classmethod` does and what do you think is a class method? – Wombatz Feb 17 '17 at 18:38
  • Stop using classmethod – juanpa.arrivillaga Feb 17 '17 at 18:38
  • 1
    Your methods will be methods anyhow, no need to use @classmethod, that's something quite different. – Jacques de Hooge Feb 17 '17 at 18:39
  • If you don't know what a particular decorator does, why would you sprinkle it throughout your code? – TigerhawkT3 Feb 17 '17 at 18:41
  • Also, this is Python. Stop using getters and setters. Also, you almost certainly do not want to make `name` and `path` class-level variables. This is not Java. Go read a tutorial on class definitions in Python. – juanpa.arrivillaga Feb 17 '17 at 18:42
  • @juanpa.arrivillaga - Technically, getters and setters are still a thing in Python with the `@property` decorator, but yes, redundant wrapper methods that add no special behavior are unnecessary. – TigerhawkT3 Feb 17 '17 at 18:44
  • 1
    @TigerhawkT3 yes, yes. It is more accurate to say "use the descriptor protocol to implement getters and setters". But baby steps, first, we need to purge the Java out of this one. – juanpa.arrivillaga Feb 17 '17 at 18:48
  • Thanks, I changed it. Thanks @juanpa.arrivillaga :) – Marco Feb 17 '17 at 18:50

1 Answers1

2

You are using __init__ method as a class method.

Using @classmethod for every method will result in a singleton, that's why the vars overwrite.

Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • If I remove `@classmethod` it seems that doesn't assign variables. The output now is: None instead the old values. – Marco Feb 17 '17 at 18:41
  • @MarcoAntonioAraújoMatarazzo **Because you made your getters (which you shouldn't be using anyway) class methods! Therefore, they get the class-level attributes `name` and `path`, which you've set to `None` explicitely. Just *remove every occurance of @classmethod in your code, and remove `name = None` and `path = None` at the top of your class. That is not how you make instance attributes!* – juanpa.arrivillaga Feb 17 '17 at 18:46