Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Cool object trick

Reply
Thread Tools

Cool object trick

 
 
dataangel
Guest
Posts: n/a
 
      12-12-2004
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.
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
 
 
 
Jive
Guest
Posts: n/a
 
      12-17-2004
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"]


 
Reply With Quote
 
has
Guest
Posts: n/a
 
      12-17-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] cool.io 0.9.0: a cool event framework for Ruby (formerly knownas Rev) based on libev Tony Arcieri Ruby 14 12-09-2010 05:23 AM
Cool computer, no I really mean cool Collector»NZ NZ Computing 13 04-24-2005 02:45 AM
Re: Cool object trick Alex Stapleton Python 8 12-18-2004 08:35 AM
RE: Cool object trick Doran_Dermot@emc.com Python 4 12-17-2004 10:49 PM
Cool Applet Cool Applet http://24.60.235.213/Grid/ Alisher Fatykhov Java 1 08-06-2003 04:32 AM



Advertisments