Hi, what am I doing wrong here

I'm developing a wxPython app, but it's Python itself that's giving me trouble I'm afraid.

I made a test application to show what I'm trying to achieve. (And also because I had hoped that I would have found the error in my understanding of Python myself)

class Test:
    def __init__(self, Registration=[]):
        self.Registrations = Registration
     def addRegistration(self, Registration):
        self.Registrations.append(Registration)
               print self.Registrations

t = Test()
t.addRegistration('item 1')

u = Test()
u.addRegistration('item 2')

The test app behaves exactly as my real app. This is the output:

['item 1']
['item 1', 'item 2']

This is what I had expected:

['item 1']
['item 2']

I had expected self.Registrations to be bound to one specific instance. Instead it grows for all instance at the same time. I had expected this behaviour if I had defined it under the class, instead I defined it under my __init__. Shouldn't it be private to the one instance?

I hope you can help,

Many thanks,

Jo

Jo a écrit :

I'm developing a wxPython app, but it's Python itself that's giving me trouble I'm afraid.

<snip>

class Test:
   def __init__(self, Registration=):
       self.Registrations = Registration

<snip>

I had expected self.Registrations to be bound to one specific instance. Instead it grows for all instance at the same time. I had expected this behaviour if I had defined it under the class, instead I defined it under my __init__. Shouldn't it be private to the one instance?

You just fell into this trap:

Hope this helps,

···

--
Amaury

Jo wrote:

I'm developing a wxPython app, but it's Python itself that's giving me trouble I'm afraid.

I made a test application to show what I'm trying to achieve. (And also because I had hoped that I would have found the error in my understanding of Python myself)

class Test:
   def __init__(self, Registration=):
       self.Registrations = Registration

You're right, it's Python itself.

The way to do this is

     def __init__ (self, Registration=None):
         if Registration is None:
             Registration =
... and so on.

Default arguments are bound to the function when the 'def'
statement is executed.. resulting in a single list value
that starts empty, but is referenced by every class instance,
It's almost a class variable, although it's bound in the
class's __init__'s namespace, rather than the class's own
namespace.

The corrected code arranges to create a fresh empty list
every time __init__ is called, giving you what you expect.
None is the customary stand-in but it could be any value that's
'out-of-band', that is, distinct from all desirable values for
Registration.

This is the most prevalent Python booby-trap there is.

         Cheers, Mel.

Amaury Forgeot d'Arc wrote:

Jo a écrit :

I had expected self.Registrations to be bound to one specific instance. Instead it grows for all instance at the same time. I had expected this behaviour if I had defined it under the class, instead I defined it under my __init__. Shouldn't it be private to the one instance?

You just fell into this trap:
General Python FAQ — Python 3.13.0 documentation

Hope this helps

Thank you, thank you, thank you and merci beaucoup!!!

This was exactly what was going on. I'm glad that snatch is out now,

Bonne nuit,

Jo