Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 2nd, 2006, 12:55 PM
Tomi Lindberg
Guest
 
Posts: n/a
Default Class definition within function

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 :)
Quote:
Quote:
Quote:
>>def f():
class C(object):
def __init__(self):
self.a = 'a'
return C()
Quote:
Quote:
Quote:
>>x = f()
>>x.a
'a'
Quote:
Quote:
Quote:
>>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'
Quote:
Quote:
Quote:
>>>
--
Tomi Lindberg
  #2  
Old August 2nd, 2006, 01:05 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: Class definition within function

Tomi Lindberg wrote:
Quote:
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 :)
>
Quote:
Quote:
>>def f():
class C(object):
def __init__(self):
self.a = 'a'
return C()
>
Quote:
Quote:
>>x = f()
>>x.a
'a'
Quote:
Quote:
>>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
  #3  
Old August 2nd, 2006, 01:05 PM
Kay Schluehr
Guest
 
Posts: n/a
Default Re: Class definition within function


Tomi Lindberg wrote:
Quote:
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)?
def f():
class C(object):
def __init__(self):
self.a = 'a'
f.C = C
return C()
Quote:
Quote:
Quote:
>>f.C
<class '__main__.C'>
Quote:
And yes, I think this is one of those times
when the real question is why :)
Definitely ;)

  #4  
Old August 2nd, 2006, 01:05 PM
Tomi Lindberg
Guest
 
Posts: n/a
Default Re: Class definition within function

Diez B. Roggisch wrote:
Quote:
No, its not. Only inside of it. And the question really is: why?
Thanks. And no need to worry, the question was intended as
fully theoretical.

--
Tomi Lindberg
  #5  
Old August 2nd, 2006, 01:15 PM
Peter Otten
Guest
 
Posts: n/a
Default Re: Class definition within function

Tomi Lindberg wrote:
Quote:
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 = type(x)()

By the way you get an instance of a different class C every time you call f,
so that

isinstance(f(), type(f())

is False.

Peter
  #6  
Old August 2nd, 2006, 01:15 PM
Duncan Booth
Guest
 
Posts: n/a
Default Re: Class definition within function

Tomi Lindberg wrote:
Quote:
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 :)
>
Quote:
Quote:
>def f():
class C(object):
def __init__(self):
self.a = 'a'
return C()
>
Quote:
Quote:
>x = f()
>x.a
'a'
Quote:
Quote:
>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'
Quote:
Quote:
>>
>
Well, you could use 'type(x)()', or object.__subclasses__() will include C
for as long as the class actually exists. Choosing the correct C from
object's subclasses could prove somewhat tricky though: remember you'll get
a new C class every time you call 'f'.
  #7  
Old August 2nd, 2006, 01:35 PM
Tomi Lindberg
Guest
 
Posts: n/a
Default Re: Class definition within function

Peter Otten wrote:
Quote:
By the way you get an instance of a different class C every time you call f,
so that
>
isinstance(f(), type(f())
>
is False.
That I didn't know. Well, that theory won't be seeing much
practice I guess.

--
Tomi Lindberg
  #8  
Old August 2nd, 2006, 01:45 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: Class definition within function

Kay Schluehr wrote:
Quote:
>
Tomi Lindberg wrote:
Quote:
>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)?
>
def f():
class C(object):
def __init__(self):
self.a = 'a'
f.C = C
return C()
>
Quote:
Quote:
>>>f.C
<class '__main__.C'>
Not working, unless f has been called at least once. But what I didn't know
(and always wondered) if the classdefinition inside a function can use the
outer scope - and apparently, it can! Cool.


def f(baseclass):
class C(baseclass):
def __init__(self):
self.a = 'a'
f.C = C
def foo(self):
print baseclass
return C()

c = f(object)
print f.C

c.foo()


Diez
  #9  
Old August 2nd, 2006, 03:15 PM
Kay Schluehr
Guest
 
Posts: n/a
Default Snake is eating itself

Diez B. Roggisch wrote:
Quote:
Kay Schluehr wrote:
>
Quote:

Tomi Lindberg wrote:
Quote:
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)?
def f():
class C(object):
def __init__(self):
self.a = 'a'
f.C = C
return C()
Quote:
>>f.C
<class '__main__.C'>
>
Not working, unless f has been called at least once.
Right.
Quote:
But what I didn't know
(and always wondered) if the classdefinition inside a function can use the
outer scope - and apparently, it can! Cool.
Yes, the bytecode simply refers to a global name f in the scope of
__init__. Fortunately the Python compiler is too stupid to guess what f
might be but simply expects that f exists at runtime. I use this
"snake is eating itself" pattern very often for passing a module as a
reference to another module inside of itself:

--------------------- Module M
import B

def eatMe():
import M
B.m = M

if __name__ == '__main__':
eatMe()

-----------------------------------

One might consider M as a component which is definig stuff. Once you
select such a component it can be used to configure a framework F of
which B is a part ( B doesn't import M ! ) So F is essentially
parametrized by M. You ere entering the next level when different
parametrizations F(M1), F(M2),... can coexist or even refer to each
other. This isn't just a mental exercise but it's going to be the way
EasyExtend will support multiple extension languages at the same time
in the fist beta version of the program. Yes, I know ... everything is
heavily cyclic and we should do hierarchies instead ;)

  #10  
Old August 2nd, 2006, 04:05 PM
Rick Zantow
Guest
 
Posts: n/a
Default Re: Class definition within function

Duncan Booth <duncan.booth@invalid.invalidwrote in
news:Xns9813882C9C0FDduncanbooth@127.0.0.1:
Quote:
Quote:
Quote:
>>def f():
> class C(object):
> def __init__(self):
> self.a = 'a'
> return C()
>>
Quote:
>>x = f()
>>x.a
>'a'
Quote:
>>y=f.C()
>>
>
Of course there's this:
Quote:
Quote:
Quote:
>>def f():
.... class C(object):
.... def __init__(self):
.... self.a = 'a'
.... return C()
....
Quote:
Quote:
Quote:
>>x = f()
>>x.a
'a'
Quote:
Quote:
Quote:
>>y=x.__class__()
>>y.a
'a'
Quote:
Quote:
Quote:
>>type(y) == type(x)
True

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles