Constructor re-initialization issue

P

pythonreptile

Hello all,

I have come across this issue in Python and I cannot quite understand
what is going on.

class Param():
def __init__(self, data={}, condition=False):
if condition:
data['class']="Advanced"
print data

In the previous example, I expect the variable data to be re-
initialized every time I construct an object type Param. However, when
I do the following:

Param(condition=True)
Param(condition=False)

The second call still prints {'class': 'Advanced'}

Shouldn't data be initialized to {} since it is the default in
__init__? Why would the state of data be preserved between two
independent instantiations?

Any help would be greatly appreciated.

M.
 
M

Mike Kent

Hello all,

I have come across this issue in Python and I cannot quite understand
what is going on.

class Param():
def __init__(self, data={}, condition=False):
if condition:
data['class']="Advanced"
print data

In the previous example, I expect the variable data to be re-
initialized every time I construct an object type Param. However, when
I do the following:

Param(condition=True)
Param(condition=False)

The second call still prints {'class': 'Advanced'}

Shouldn't data be initialized to {} since it is the default in
__init__? Why would the state of data be preserved between two
independent instantiations?

Any help would be greatly appreciated.

M.

This is a Frequently Asked Question (FAQ):
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects
 
G

George Sakkis

Hello all,

I have come across this issue in Python and I cannot quite understand
what is going on.

class Param():
def __init__(self, data={}, condition=False):
if condition:
data['class']="Advanced"
print data

In the previous example, I expect the variable data to be re-
initialized every time I construct an object type Param. However, when
I do the following:

Param(condition=True)
Param(condition=False)

The second call still prints {'class': 'Advanced'}

Shouldn't data be initialized to {} since it is the default in
__init__? Why would the state of data be preserved between two
independent instantiations?

Any help would be greatly appreciated.

M.

This must be by far the most FAQ.. unfortunately it seems it will
remain for 3.x as well: http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects
 
P

pythonreptile

Hello all,
I have come across this issue in Python and I cannot quite understand
what is going on.
class Param():
def __init__(self, data={}, condition=False):
if condition:
data['class']="Advanced"
print data
In the previous example, I expect the variable data to be re-
initialized every time I construct an object type Param. However, when
I do the following:

The second call still prints {'class': 'Advanced'}
Shouldn't data be initialized to {} since it is the default in
__init__? Why would the state of data be preserved between two
independent instantiations?
Any help would be greatly appreciated.

This must be by far the most FAQ.. unfortunately it seems it will
remain for 3.x as well:http://www.python.org/doc/faq/general/#why-are-default-values-shared-...
Thanks for clearing this up for me.

M.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top