473,387 Members | 1,669 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,387 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 5318
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.