Max Rybinsky <[EMAIL PROTECTED]> wrote: > Thank you for explanation, Alex. > It appears that almost every beginner to Python gets in trouble with > this ...feature. :)
Almost every beginner to Python gets in trouble by expecting "do what I'm thinking of RIGHT NOW"-binding, which no language offers: in other words, such beginners sometimes expect late binding where Python binds early, and, vice versa, they at other times expect early binding where Python binds late. Not ALWAYS, mind you -- what they expect depends on what would appear to them to be most convenient for their immediate needs on each separate occasion. Some other languages try to follow beginners and offer "do what I mean" semantics -- when using such languages, one ends up in a battle of wit against the compiler's guesses about one's intentions. Python instead offers extremely simple rules, such as: any name is looked up each and every time it's evaluated (and at no other times); evaluation of function headers happens completely at the time the 'def' or 'lambda' evaluates, while evaluation of function bodies happens completely at the time the function is _called_. By learning and applying such simple rules there can be no surprise about what is evaluated (and, in particular, looked up) when. E.g., consider the difference between the following two functions: def early(x=whatever()): ... def late(): x=whatever() ... In 'early', the call to whatever() is part of the function's header, and therefore happens at the time the 'def' statement executes -- and thus name 'whatever' means whatever it means at THAT time (if at that time it's not bound to anything, the 'def' statement fails with an exception). In 'late', the call to whatever() is part of the function's body, and therefore happens each time the function is called -- and thus name 'whatever' means whatever it means at THAT time (if at that time it's not bound to anything, the call fails with an exception). Alex -- http://mail.python.org/mailman/listinfo/python-list