473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Anonymous class question

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
3 5317
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
Bengt Richter wrote:
On Thu, 07 Aug 2003 03:20:24 GMT, Carl Banks <im*****@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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ed Severn | last post by:
I'm sorry of this question has been posed and answered many times before. But I have avoided using the "package" statement because of this. Most of my classes have no "package" statement, and...
0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
3
by: anonymous | last post by:
I believe I ran into an interesting way to create memory leaks in C# 2.0 using anymous delegates. Here is a sample of the code in question. private void Handle_Event(object sender, EventArgs e)...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
4
by: josh | last post by:
Hi, is there the possibility to create anonymous class (not object) in C++ like do Java? as an example: in Java if I do: // here Shape is an Interface that is like a c++ class wih only pure...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
0
by: kreismaler | last post by:
I have some problems to understand the difference of using the STDOUT and using "anonymous pipes" as shown below: using System; using System.Diagnostics; using System.IO; namespace...
1
by: ktrvnbq02 | last post by:
Hi, I recently came to debug some old code that was causing a StackOverflowException. The code in question makes significant use of recursion and with large data structures it exhausted the...
22
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.