Namespaces in functions vs classes

Ethan Furman ethan at stoneleaf.us
Sun Apr 17 22:25:23 CEST 2011


Gerald Britton wrote:
> However,  I would like a deeper
> understanding of why I cannot use "foo" as an unqualified variable
> inside the method in the class.  If Python allowed such a thing, what
> problems would that cause?

8<----------------------------------------------------
"script with possible name clashes"

eggs = 'scrambled eggs'
meat = 'steak'

class Breakfast():
     meat = 'spam'
     def serve(self):
         print("Here's your %s and %s!" %
                (eggs, meat))

Breakfast().serve()
8<----------------------------------------------------

What will serve print?

~Ethan~



More information about the Python-list mailing list