[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

correlation between seemingly independent instances of same class

nikipore

2/20/2008 7:04:00 PM

Hello,

the results of the following code boggle me. i would have expected
that y.data is not affected by setting z.data.

Regards Jan


--code---------------------------------------------
class Test:
def __init__(self, x={}):
self.data = x
#end def
#end class Test

y = Test()
z = Test()

print 'y.data = ',y.data
print 'z.data = ',z.data

z.data['B'] = 2 # this statement also sets y.data
z.data= {'A': 1} # this statement doesn't

print 'y.data = ',y.data
print 'z.data = ',z.data
--code end-------------------------------------------

--output-----------------------------------------------
>"C:\Program Files\Python25\pythonw" -u "thestrangething.py"
y.data = {}
z.data = {}
y.data = {'B': 2}
z.data = {'A': 1}
4 Answers

Dennis Lee Bieber

2/20/2008 7:25:00 PM

0

On Wed, 20 Feb 2008 11:04:26 -0800 (PST), nikipore@googlemail.com
declaimed the following in comp.lang.python:

I guess it /has/ been about two weeks since this bit the last
person...


> def __init__(self, x={}):

def __init__(self, x=None):
if x:
self.data = x
else:
self.data = {}

Python Reference Manual, section 7.5, Function Definitions
"""
....
Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that that same ``pre-computed'' value is used
for each call. This is especially important to understand when a default
parameter is a mutable object, such as a list or a dictionary: if the
function modifies the object (e.g. by appending an item to a list), the
default value is in effect modified. This is generally not what was
intended. A way around this is to use None as the default, and
explicitly test for it in the body of the function, e.g.:
....
"""
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Michael Ejercito

4/3/2014 1:20:00 PM

0



"The Revd" wrote in message
news:242oj9porh1s72tmsgd07i4mj7of971maj@4ax.com...

>On Sun, 30 Mar 2014 11:19:13 -0700, "Michael Ejercito"
><mejercit@hotmail.com> wrote:

>>
>>
>>"The Revd" wrote in message
>>news:0fjgj99e0a61dt5g4thrlsktnv0ejqspp6@4ax.com...
>>
>>>On Sun, 30 Mar 2014 09:40:09 -0700, "Michael Ejercito"
>>><mejercit@hotmail.com> wrote:
>>>> I suck no rectums.
>>
>>>You DO.
>> No, I do not, you mangina.

>Yes, you DO, you craven gook anilinguist!
Nithing, I suck no rectums.


I doubt that you fuck vaginas.


The following ballad was dedicated to me.

http://groups.google.com/group/soc.veterans/msg/2a544b...

Well Michael is the guardian angel of Israel,
He tackles the anti-Semites that stink and smell,
He attacks them for what they are,
And he???s not a drunk sitting in a bar,
He rebuffs the Nazi and heathen scum,
He a clean sort of fellow and not a bum.
He???s courageous and brave as can be,
And he chastises the Revd for jacking his pee-pee.
Now we all know Michael is a good guy,
As we laugh at the anti-Semitics that step in cow-pie.
Well Michael Ejercito is an honorable man,
As he out smarts the Nazis of the Klu-Klux-Klan.
If he is a Jew or if he???s not,
He???s unlike the Revd that jacks his pecker snot.
Yes Michael seems to be a pretty good dude,
His mind is clean but the Revd???s is lude.
Michael we???re glad you???re on our side,
You???re a good example of Jewish pride.
The anti-Semites and the Huns are mad,
However, Michael when you post you makes people feel glad.
Dear Mike, Keep up the good work and remain well in mind and body,
While the Revd jacks his meat and washes in his pody.
Well that is all I have to say about Michael Ejercito the Gebor (??????)
The Revd is a homo and his wife is a whore.

The Revd

4/3/2014 11:55:00 PM

0

On Thu, 3 Apr 2014 06:19:32 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>
>"The Revd" wrote in message
>news:242oj9porh1s72tmsgd07i4mj7of971maj@4ax.com...
>
>>On Sun, 30 Mar 2014 11:19:13 -0700, "Michael Ejercito"
>><mejercit@hotmail.com> wrote:
>
>>>
>>>
>>>"The Revd" wrote in message
>>>news:0fjgj99e0a61dt5g4thrlsktnv0ejqspp6@4ax.com...
>>>
>>>>On Sun, 30 Mar 2014 09:40:09 -0700, "Michael Ejercito"
>>>><mejercit@hotmail.com> wrote:
>>>>> I suck no rectums.
>>>
>>>>You DO.
>>> No, I do not, you mangina.
>
>>Yes, you DO, you craven gook anilinguist!
> Nithing, I suck no rectums.

Gook, you suck any and every jew rectum within sucking distance.

> I doubt that you fuck vaginas.

I KNOW that your needledick wouldn't even touch the walls of a vagina.

The Revd

4/4/2014 2:30:00 AM

0

On Thu, 3 Apr 2014 06:19:32 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>
>"The Revd" wrote in message
>news:242oj9porh1s72tmsgd07i4mj7of971maj@4ax.com...
>
>>On Sun, 30 Mar 2014 11:19:13 -0700, "Michael Ejercito"
>><mejercit@hotmail.com> wrote:
>
>>>
>>>
>>>"The Revd" wrote in message
>>>news:0fjgj99e0a61dt5g4thrlsktnv0ejqspp6@4ax.com...
>>>
>>>>On Sun, 30 Mar 2014 09:40:09 -0700, "Michael Ejercito"
>>>><mejercit@hotmail.com> wrote:
>>>>> I suck no rectums.
>>>
>>>>You DO.
>>> No, I do not, you mangina.
>
>>Yes, you DO, you craven gook anilinguist!
> Nithing, I suck no rectums.

Gook, you suck any and every jew rectum within sucking distance.

> I doubt that you fuck vaginas.

I KNOW that your needledick wouldn't even touch the walls of a vagina.