Tomi Lindberg wrote:

> Hi,
> 
> With the following function definition, is it possible to
> create an instance of class C outside the function f (and if
> it is, how)? And yes, I think this is one of those times
> when the real question is why :)
> 
>  >>> def f():
> class C(object):
> def __init__(self):
> self.a = 'a'
> return C()
> 
>  >>> x = f()
>  >>> x.a
> 'a'
>  >>> y=f.C()
> 
> Traceback (most recent call last):
>    File "<pyshell#22>", line 1, in -toplevel-
>      y=f.C()
> AttributeError: 'function' object has no attribute 'C'

No, its not. Only inside of it. And the question really is: why? If you need
a class that can be instantiated regardless of the execution of f, make it
a globally visible class. If it depends on something f computes, make it a
function-local one (if you like)

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to