471,351 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Unexpected python behaviour

Consider the script:

def test(drivers={}):
if drivers:
print drivers
else:
drivers["A"] = "B"

if __name__ == "__main__":
test()
test()

prints in stdout:
{'A': 'B'}

I expected that nothing would be displayed.
I do not understand this behaviour (python2.3 on WinXP and Linux).

If anybody can explain this situation, you are welcome.

Thank you.
--
================================================== ==============
Dr. Richard PHILIPS
University of Antwerp
Systemmanager Anet

Phone: +32 3 820.21.53
Fax: +32 3 820.21.59
GSM: 0478/36.76.28
Email: Ri*************@ua.ac.be
================================================== ==============


Jul 18 '05 #1
2 1337
Richard Philips <ri*************@ua.ac.be> wrote in news:ma*************************************@pytho n.org:
Consider the script:

def test(drivers={}):
if drivers:
print drivers
else:
drivers["A"] = "B"

if __name__ == "__main__":
test()
test()

prints in stdout:
{'A': 'B'}

I expected that nothing would be displayed.
I do not understand this behaviour (python2.3 on WinXP and Linux).
You are not alone, this is one of the commonest questions from beginners.

If anybody can explain this situation, you are welcome.
The best explanation I know is in the FAQ, entry 4.21.

See http://www.python.org/doc/faq/genera...etween-objects

Thank you.

You are welcome.

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2
Richard Philips wrote:
Consider the script:

def test(drivers={}):
if drivers:
print drivers
else:
drivers["A"] = "B"

if __name__ == "__main__":
test()
test()

prints in stdout:
{'A': 'B'}

I expected that nothing would be displayed.
I do not understand this behaviour (python2.3 on WinXP and Linux).

If anybody can explain this situation, you are welcome.

That caught me by suprise to, especially because after running it,
'drivers' is not defined

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Simon Wittber | last post: by
2 posts views Thread by Gerhard Esterhuizen | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
3 posts views Thread by rimmer | last post: by
3 posts views Thread by leonhard.vogt | last post: by
5 posts views Thread by sukkopera | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.