Unexpected python behaviour
Duncan Booth
duncan at NOSPAMrcp.co.uk
Fri Nov 28 10:11:45 EST 2003
Richard Philips <richard.philips at ua.ac.be> wrote in news:mailman.1174.1070029879.702.python-list at python.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/general.html#why-are-default-values-shared-between-objects
>
> Thank you.
>
You are welcome.
--
Duncan Booth duncan at 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?
More information about the Python-list
mailing list