473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Constructor re-initialization issue

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.
Jun 27 '08 #1
3 775
On Jun 3, 6:11 pm, pythonrept...@gmail.com wrote:
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/genera...etween-objects
Jun 27 '08 #2
On Jun 3, 6:11 pm, pythonrept...@gmail.com wrote:
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/genera...etween-objects
Jun 27 '08 #3
On Jun 3, 12:59 pm, George Sakkis <george.sak...@gmail.comwrote:
On Jun 3, 6:11 pm, pythonrept...@gmail.com wrote:
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/genera...values-shared-...
Thanks for clearing this up for me.

M.

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
8
by: Christian Brechbühler | last post by:
I get a compile time error that I don't understand. ======================================= begin constructor.cc ====== struct Negate {}; class A { int i; public: A(double x ):...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
4
by: Kench | last post by:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1 hour ago still it does not show up there so posting this here. Hi, Consider class A & B both of which implement a copy...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
2
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
9
by: Thomas Scheiderich | last post by:
I am curious as to 2 things. One is the order that the constructors are handled and the other is why the debugger doesn't show the static objects constructor (unless you set a breakpoint. For...
8
by: JAL | last post by:
According to MSDN2, if a managed class throws an exception in the constructor, the destructor will be called. If an exception is thrown in the constructor the object never existed so how in the...
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
14
by: Sweeya | last post by:
/*Program to do manipulations on a string*/ #include <iostream> using namespace std; class String { int len; char *p;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.