Anonymous class question

D

Dan Williams

Python experts,

Is there a more pythonic way to do something evquilent to what this line
does without creating a dummy class?

self.file = type("", (object,), {'close':lambda slf: None})()

As you can guess, I want a dummy object that I can call close on with
impunity.

I've been reading _Python in a Nutshell_ (Thanks, Alex!), which defines
a Bunch class that I think could also work, but if I'm only going to use
it to define a class with a dummy close() method, I might as well just
create the dummy class with the dummy method. . . Unless there is
something equvilent to Bunch in one of the standard modules that I don't
know about . . .?
 
C

Carl Banks

Dan said:
Python experts,

Is there a more pythonic way to do something evquilent to what this line
does without creating a dummy class?

self.file = type("", (object,), {'close':lambda slf: None})()

As you can guess, I want a dummy object that I can call close on with
impunity.

The above line does create a dummy class. What's your motivation for
doing this--what's wrong with defining a dummy class? Is there a
reason you want it to be nameless? Do you want to do this simply to
save typing? Do you have hundreds of these dummy classes and don't
want to type a new class each time? The Pythonic way is to wrap it in
a function:


def nameless_dummy_object_with_methods(*methods):
d = {}
for sym in methods:
d[sym] = lambda self,*args,**kwargs: None
return type("",(object,),d)()


self.file = nameless_dummy_object_with_methods('close')


You specify any methods you want as strings and the functions builds a
class with those methds, which quietly accept any arguments they are
passed, and returns an instance of it. Presumably, the code works
with regular objects, and trying to define the right "prototype" for
each method is doing unnecessary work.
 
D

Dan Williams

Bengt said:
Does that (object,) do something I'm missing?

(<type 'object'>,)

Regards,
Bengt Richter

I thought it made it a new-style class. I could be wrong about that,
though. . .

-Dan
 
A

Alex Martelli

Dan Williams wrote:
...
I thought it made it a new-style class. I could be wrong about that,
though. . .

Class whose metaclass is type (and it surely will be, if you
instantiate type directly by calling it as you did) ARE "new
style" by definition. Don't be confused with the class
statement: _then_ you may need to explicitly specify object
as a base class [or otherwise set the __metaclass__] in order
to have a class be new-style rather than the default 'classic'.
But when you explicitly call type, specifiying object as the
only base is not necessary (although, it _is_ innocuous).


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top