473,407 Members | 2,359 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,407 software developers and data experts.

Cool object trick

I read some discussion on this list before about how sometimes it's
useful to create a generic object class that you can just stick
attributes to. I was reading the PyPanel source (not written by me) and
I came across this:

#----------------------------------------------------------------------------

class Obj(object):
#----------------------------------------------------------------------------

""" Multi-purpose class """
#----------------------------
def __init__(self, **kwargs):
#----------------------------
self.__dict__.update(kwargs)

Normally I'd just use class Obj(object): pass, but the advantage to this
method is you can create an Obj like this:

Obj(id="desktop", last=0, color=self.getColor(DESKTOP_COLOR))

You can pass all the attributes you want the object to have this way.
Nifty :)

Sorry if this has been posted before, but I haven't seen it.
Jul 18 '05 #1
3 1418
dataangel wrote:
Normally I'd just use class Obj(object): pass, but the advantage to this
method is you can create an Obj like this:

Obj(id="desktop", last=0, color=self.getColor(DESKTOP_COLOR))

You can pass all the attributes you want the object to have this way.
Nifty :)


Yup, that's basically what I was proposing in the pre-PEP:

http://mail.python.org/pipermail/pyt...er/252621.html

I've let it slide for a bit here, but my intent is to make a patch that
puts something like the pre-PEP class into the collections module and
then send in the PEP. I've been kinda busy recently, and it's not quite
trivial since Bunch is in Python and currently the collections module is
in C, but hopefully in the next few weeks I'll have the time.

Steve
Jul 18 '05 #2
Kinda cool.

It's occured to me that just about everything Pythonic can be done with
dicts and functions. Your Obj is just a dict with an alternate syntax. You
don't have to put quotes around the keys. But that's cool.
class struct(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

# Indented this way, it looks like a struct:
obj = struct( saying = "Nee"
, something = "different"
, spam = "eggs"
)

print obj.spam

# Is that really much different from this?

obj2 = { "saying" : "Nee"
, "something" : "different"
, "spam" : "eggs"
}

print obj2["spam"]
Jul 18 '05 #3
has
Jive wrote:
# Is that really much different from this?


Functionally, no. However it can help make code more readable when
dealing with complex data structures, e.g. compare:

obj.spam[1].eggs[3].ham

to:

obj["spam"][1]["eggs"][3]["ham"]

I've used it a couple times for this particular reason and it
definitely has its niche; though I'm not sure it's sufficiently common
or useful to justify its inclusion it in the standard library, and it's
trivial to whip up as-and-when it's needed.

BTW & FWIW, I think the usual name for this kind of structure is
'record' (or 'struct' in C).

HTH

has

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
0
by: John Bailo | last post by:
I just read a great article on object serialization in .NET http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=236 Being able to tag member in an object as Xml elements is...
1
by: Kristian Kjems | last post by:
Look at the Code Snippet below: It is not possible to Deserialize the Animal in animalStr directly because the type of the animal is not known. Deserializing into the abstract type will not work...
7
by: Carsten H. Pedersen | last post by:
Hello I want to create a kind of shared object. I have created a class, Counter, which you can then import. Through the Counter class, i would like to have access to a shared object. Lets say...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
2
by: HeroinNO.4 | last post by:
Copy the code below and save in a .htm file, for example : 1.htm, then run it in browser, you'll see a cool count down timer ! If it doesn't work, you may open http://www.fillweb.com in IE and...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, In my vb2005 app I create 3 datatables in the Datasource area, tbl1, tbl2, tbl3. tbl1 contains 1 column, tbl2 contains 2 columns tbl3 contains 3 columns. Based on selections from a...
0
by: [Cool staff!||Hi! I think this need for || Help me | last post by:
http://con-cern.org/files/2007/5/xenical-21024312.html cheap xenical http://con-cern.org/files/2007/5/auto-21024411.html auto loan refinance ...
2
by: Hoss | last post by:
I created this cool game. It was made entirely with JavaScript (and a tiny bit of PHP) <a href="http://beem.awardspace.com">http://beem.awardspace.com</a> Snippet from the explanation ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.