class object access in class definition?

Werner Schiendl ws-news at gmx.at
Wed Mar 7 04:06:22 EST 2001


Depends on what you really need this for

But you can of course access the class object in the __init__ method
like this

>>> class Foo:
...  def __init__(self):
...   print Foo
...

hth
werner

Lars Damerow <lars at pixar.com> wrote in message
news:Pine.LNX.4.21.0103061556510.2015-100000 at zero...
> Hi folks!
>
> Is it possible to reference a class from within its own definition?
Something
> along the lines of:
>
> class Foo:
>     print Foo
>
> This works for functions--that is,
>
> >>> def foo():
> ...     print foo
> ...
> >>> foo()
> <function foo at 0x8111024>
>
> works as expected, but the class definition above raises a NameError:
>
> >>> class Foo:
> ...     print Foo
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 2, in Foo
> NameError: There is no variable named 'Foo'
>
> Thanks for any pointers!
> -lars
>
> ___________________________________________________________
> lars damerow
> senior unix administrator
> pixar animation studios
> lars at pixar.com
>
> "Nauseous.  Nauseated.  The first means 'sickening to contemplate'; the
second
> means 'sick at the stomach.'  Do not, therefore, say, "I feel nauseous,"
unless
> you are sure you have that effect on others."
>
> - William Strunk Jr., "The Elements of Style"
>





More information about the Python-list mailing list