[Tutor] is it legal to have a class within a def
bgailer at alum.rpi.edu
Thu Jan 3 06:31:19 CET 2008
> def someMethod():
> class MyClass(object):
> if something:
> return someval
Legal? Well the police won't come after you!
Python allows a class statement anywhere. So this use is part of the
So the question becomes "why would you want to do that?"
1 - this usage creates a local object MyClass. The object will not be
visible outside the function. The class definition is executed each time
the function is called.
2 - the object could be returned or assigned to a global or added to a
parameter that is a collection.
3 - if no instances are created in the function the object will
disappear once the function execution ends.
More information about the Tutor