[Tutor] is it legal to have a class within a def

Alan Gauld alan.gauld at btinternet.com
Thu Jan 3 09:15:41 CET 2008


"johnf" <jfabiani at yolo.com> wrote 

> def someMethod():
>   class MyClass(object):
>       .....
>  if something:
>     .....
>     return someval


Did you try it?

>>> def f():
...   class C: pass
...   return C
...
>>> def g(x):
...   class C: pass
...   if x == 42:
...     return C
...   else: return 666
...
>>> c = f()
>>> c
<class __main__.C at 0x7ff1bbfc>
>>>

It works for me...


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list