Trying to understand a little python

McCarty, Greg Greg.McCarty at mantech-ist.com
Mon Dec 6 14:26:57 EST 2004


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?

 

Line 10 - Seems that the syntax 'Tester()' actually causes the __init__
method to 

fire.  Is this the only case?

 

Line 12 - At this point, I was thinking of Tester.stuff as a static
variable

of the Tester class.

 

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

 

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


More information about the Python-list mailing list