473,381 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 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 1398
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon Wittber | last post by:
Greetings fellow Python People! I am using the latest debian-unstable Apache 1.3 and mod-python 2.7 with python 2.3.3 Yesterday I wrote a small mod-python script which returned the URL...
2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
3
by: rimmer | last post by:
I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats. ---------...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
3
by: leonhard.vogt | last post by:
Please consider that example: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32 Type "help", "copyright", "credits" or "license" for more information. 'foo' 'bar' Traceback (most recent call...
23
by: gu | last post by:
hi to all! after two days debugging my code, i've come to the point that the problem was caused by an unexpected behaviour of python. or by lack of some information about the program, of course!...
5
by: Ivan Velev | last post by:
Hello, Minimal example below - it gives me different output if I comment / uncomment the extra time.mktime call - note that this call is not related in any way to main logic flow. When...
5
by: sukkopera | last post by:
Hi, I have just encountered a Python behaviour I wouldn't expect. Take the following code: ------------------------------------------------------------------------ class Parent: a = 1 def m...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.