Class behaving like a static class

G

gu

Hi to all,
i have a module (a single .py file, actually), with a class called
HashedDir.

when i import the file and instanciate 2 instances of that class, when i
check the object's fields they're always the same, even if the two
objects should be different.

Eg:

h1 = HashedDir('someValue')
print h1.value # someValue
h2 = HashedDir('someOtherValue')
print h1.value # someOtherValue
print h2.value # someOtherValue

Any idea?
Thanks in advance
 
B

Bruno Desthuilliers

gu a écrit :
Hi to all,
i have a module (a single .py file, actually), with a class called
HashedDir.

when i import the file and instanciate 2 instances of that class, when i
check the object's fields they're always the same, even if the two
objects should be different.

Eg:

h1 = HashedDir('someValue')
print h1.value # someValue
h2 = HashedDir('someOtherValue')
print h1.value # someOtherValue
print h2.value # someOtherValue

Any idea?

Yes : post the relevant code - or at least the minimal code that
reproduces the unexpected behaviour.

There are quite a couple gotchas regarding class-vs-instance-attributes
and default-params, but none of them matches what you have here - at
least if 'value' is an attribute set on 'self' from within the
initializer. So either the above snippet is made up and you're in fact
having this problem with mutable attributes (like 'value' is a list and
you're appending to it) or there's something else in your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top