A Trivial Question

Chris Angelico rosuav at gmail.com
Thu Sep 29 00:27:31 EDT 2011


On Thu, Sep 29, 2011 at 7:53 AM, Tayfun Kayhan
<tayfun92_kayhan at yahoo.com> wrote:
> def trial():
>    class Foo(object):
>       def __init__(self):
>          print("Hello, world!")
> trial() # not worked, as expected.

Python doesn't have "class declarations" in the way that some other
languages do. The 'class' statement is an executable statement - it
builds a class object and binds it to the name Foo, just like any
other assignment. The 'def' command builds a function object in a
similar fashion. So in your trial() function, you're defining a class
and binding it to the local name Foo; and then doing nothing with it.
It's perfectly legal, and as Chris Rebert suggests, you can make it
useful by instantiating Foo() objects inside trial().

ChrisA



More information about the Python-list mailing list