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. 3 773
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ):...
|
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...
|
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...
|
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...
|
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:
|
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...
|
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...
|
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...
|
by: Sweeya |
last post by:
/*Program to do manipulations on a string*/
#include <iostream>
using namespace std;
class String
{
int len;
char *p;
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |