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)
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'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?
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.
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?