394,192 Members | 2,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 394,192 IT Pros & Developers. It's quick & easy.

Anonymous class question

Dan Williams
P: n/a
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 . . .?

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


Carl Banks
P: n/a
Dan Williams wrote:
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.
--
CARL BANKS
"You don't run Microsoft Windows. Microsoft Windows runs you."
Jul 18 '05 #2

Dan Williams
P: n/a
Bengt Richter wrote:
On Thu, 07 Aug 2003 03:20:24 GMT, Carl Banks <imbosol@aerojockey.com> wrote:

Dan Williams wrote:
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})()

Does that (object,) do something I'm missing?
>>> o1 = type('',(object,),{})()
>>> o2 = type('',(),{})()
>>> type(o1).__bases__ (<type 'object'>,) >>> type(o2).__bases__

(<type 'object'>,)

Regards,
Bengt Richter


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

-Dan

Jul 18 '05 #3

Alex Martelli
P: n/a
Dan Williams wrote:
...
self.file = type("", (object,), {'close':lambda slf: None})()

Does that (object,) do something I'm missing?
>>> o1 = type('',(object,),{})()
>>> o2 = type('',(),{})()
>>> type(o1).__bases__

(<type 'object'>,)
>>> type(o2).__bases__

(<type 'object'>,)

Regards,
Bengt Richter


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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.