Trying to understand a little python

Jeremy Jones zanesdad at bellsouth.net
Mon Dec 6 20:48:57 CET 2004


McCarty, Greg wrote:

> Ok, I'm new to python, and I'm trying to come to grips with a few 
> things.  Got
>
> lots of years of experience with Java and asp/aspx, etc.  Trying to relate
>
> Python's behavior to what I already know.
>
>  
>
> Here's the python code (line #'s added for my question) -
>
>  
>
> 01 class Tester:
>
> 02     def __init__ (self):
>
> 03         print "I'm initializing Tester"
>
> 04
>
> 05 def test(klass=Tester):
>
> 06     klass.stuff = "setting stuff"
>
> 07     print "I'm in test: " + klass.stuff
>
> 08
>
> 09 test()          # results 1: I'm in test: setting stuff
>
> 10 a=Tester()      # results 2: I'm initializing Tester
>
> 11 a.stuff         # results 3: 'setting stuff'
>
> 12 b=Tester()      # results 4: I'm initializing Tester
>
> 13 b.stuff         # results 5: 'setting stuff'
>
> 14 a.stuff="changed!"
>
> 15 b.stuff         # results 6: 'setting stuff'
>
> 16 a.stuff         # results 7:'changed!'
>
>  
>
> And here's my questions -
>
>  
>
> Line 09 - I expected the default argument assignment of line 05 to
>
> create an object of type Tester and assign it to the var klass.  Thus I
>
> expected Tester.__init__ to fire, which it didn't.  What does 
> 'klass=Tester'
>
> actually do on line 05?
>
>  
>
klass=Tester sets the default value of the variable klass to the *class* 
Tester.  This isn't creating an instance of Tester.  Only pointing klass 
to the class itself.

> Line 10 - Seems that the syntax 'Tester()' actually causes the 
> __init__ method to
>
> fire.  Is this the only case?
>
Mostly.  You can use getattr if you like.  And probably eval or exec.  
But I would try to stick with the Tester() syntax.

>  
>
> Line 12 - At this point, I was thinking of Tester.stuff as a static 
> variable
>
> of the Tester class.
>
When you set a.stuff to "changed", you are setting an instance attribute 
on "a" to "changed".  "b" is still pointing to the "static variable" on 
Tester.  Look at this:

In [12]: a = Tester()
I'm initializing Tester

In [13]: b = Tester()
I'm initializing Tester

In [14]: Tester.stuff
Out[14]: 'setting stuff'

In [15]: Tester.stuff = "FOOBAR"

In [16]: a.stuff
Out[16]: 'FOOBAR'

In [17]: b.stuff
Out[17]: 'FOOBAR'

In [18]: a.stuff = "A.STUFF"

In [19]: a.stuff
Out[19]: 'A.STUFF'

In [20]: b.stuff
Out[20]: 'FOOBAR'

I re-set Tester.stuff to "FOOBAR".  "a" and "b" attributes "stuff" were 
pointing to that for a second.  Then I pointed the attribute "stuff" on 
"a" to "FOOBAR".

>  
>
> Line 15 - We'll, I guess stuff isn't a static variable!  What is the
>
> explanation here?
>
>  
>
> Thanks for any help.
>
>  
>
> Greg McCarty
>
> **Senior Technical Advisor** / ManTech IST
>
> **ph:  410-480-9000 x2804 or 703-674-2804    fx: 410-480-0916**
>
>  
>
HTH.

Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041206/eb11ecdd/attachment.html>


More information about the Python-list mailing list