Re: Cool object trick

2004-12-18 Thread Carl Banks
Bengt Richter wrote: > m=type('',(),{})() Nick Coghlan wrote: > Heh. > > Look ma, Perlython! I doubt anyone could perform such a ghastly hack so simply and straightforwardly in Perl. -- CARL BANKS -- http://mail.python.org/mailman/listinfo/python-list

Re: Cool object trick

2004-12-18 Thread Nick Coghlan
Robert Brewer wrote: Bengt Richter wrote: >>> m=type('',(),{})() >>> m.title = 'not so fast ;-)' >>> m.title 'not so fast ;-)' Now THAT is a cool object trick. :) Heh. Look ma, Perlython! Cheers, Nick. -- Nick

RE: Cool object trick

2004-12-18 Thread Robert Brewer
Bengt Richter wrote: > >>> m=type('',(),{})() > >>> m.title = 'not so fast ;-)' > >>> m.title > 'not so fast ;-)' Now THAT is a cool object trick. :) FuManChu -- http://mail.python.org/mailman/listinfo/python-list

Re: Cool object trick

2004-12-18 Thread Fredrik Lundh
Bengt Richter wrote: > >>> m = object() > >>> m.title = 'not so fast ;-)' > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'object' object has no attribute 'title' >>> m = object() >>> m.title = 'yeah, that's stupid' Traceback (most recent call last): File "", li

Re: Cool object trick

2004-12-18 Thread Bengt Richter
On Sat, 18 Dec 2004 08:39:47 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >Carl Banks wrote: > >> For example: >> >> .def lookup_reference(key): >> . >> .return Bunch(title=title,author=author,...) >> >> The code quickly and easily returns a object with the desired key

Re: Cool object trick

2004-12-17 Thread Fredrik Lundh
Carl Banks wrote: > For example: > > .def lookup_reference(key): > . > .return Bunch(title=title,author=author,...) > > The code quickly and easily returns a object with the desired keys. > The code that calls this function would access the return values > directly, like th

Re: Cool object trick

2004-12-17 Thread Carl Banks
Alex Stapleton wrote: > Hmm true, (i had forgotten about getattr :/) in that case im indifferent > to Bunch() not that i really see why it's useful except for making code > look a bit nicer occasionaly. When using Bunch, the idea is not to access the elements indirectly. If you need to access elem

Re: Cool object trick

2004-12-17 Thread Steven Bethard
Robert Brewer wrote: Alex Stapleton wrote: you can't do var = "varA" obj = struct(varA = "Hello") print obj.var and expect it to say Hello to you. Did you mean "print obj.varA"? I believe he meant that he wanted the equivalent of: getattr(obj, var) Steve -- http://mail.python.org/mailman/listinfo/p

RE: Cool object trick

2004-12-17 Thread Robert Brewer
Alex Stapleton wrote: > you can't do > > var = "varA" > obj = struct(varA = "Hello") > print obj.var > > and expect it to say Hello to you. Did you mean "print obj.varA"? I can't think of any use case for the way you wrote it, so I'm naively guessing you've got a typo. Feel free to correct me. ;)

Re: Cool object trick

2004-12-17 Thread 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 a

Re: Cool object trick

2004-12-17 Thread Steve Holden
Fredrik Lundh wrote: Steve Holden wrote: Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon, obj.sausages, "and", obj.spam' a lot easier ;-) Of course this whole thing of substituting attribute access for dictionary keys only works as long as the keys are strings with the same

Re: Cool object trick

2004-12-17 Thread Fredrik Lundh
Steve Holden wrote: >> Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon, >> obj.sausages, "and", obj.spam' a lot easier ;-) >> > Of course this whole thing of substituting attribute access for dictionary > keys only works as long > as the keys are strings with the same synt

Re: Cool object trick

2004-12-17 Thread Steve Holden
[EMAIL PROTECTED] wrote: I rather like it! I prefer writing obj.spam to obj["spam"]! I wonder if there is a technical downside to this use of Python? P.S. Certainly makes writing 'print obj.spam, obj.spam, obj.eggs, obj.bacon, obj.sausages, "and", obj.spam' a lot easier ;-) Of course this whole t

Re: Cool object trick

2004-12-17 Thread Alex Stapleton
Steven Bethard wrote: Alex Stapleton wrote: you are setting the variable name in your code (b.varA), not generating the variable name in a string (var = "varA") (dictionary key) at run-time and fetching it from the __dict__ like i was attempting to describe. Ahh. Well if you just want to get a

Re: Cool object trick

2004-12-17 Thread Steven Bethard
Alex Stapleton wrote: you are setting the variable name in your code (b.varA), not generating the variable name in a string (var = "varA") (dictionary key) at run-time and fetching it from the __dict__ like i was attempting to describe. Ahh. Well if you just want to get an attribute, I don't se

Re: Cool object trick

2004-12-17 Thread Alex Stapleton
Steven Bethard wrote: Alex Stapleton wrote: you can't do var = "varA" obj = struct(varA = "Hello") print obj.var and expect it to say Hello to you. The Bunch object from the PEP can take parameters in the same way that dict() and dict.update() can, so this behavior can be supported like: >>> b

Re: Cool object trick

2004-12-17 Thread Steven Bethard
Alex Stapleton wrote: you can't do var = "varA" obj = struct(varA = "Hello") print obj.var and expect it to say Hello to you. The Bunch object from the PEP can take parameters in the same way that dict() and dict.update() can, so this behavior can be supported like: >>> b = Bunch({"varA":"Hello!"

Re: Cool object trick

2004-12-17 Thread Alex Stapleton
: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jive Sent: 17 December 2004 06:29 To: [EMAIL PROTECTED] Subject: Re: Cool object trick 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

RE: Cool object trick

2004-12-16 Thread Doran_Dermot
inal Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jive Sent: 17 December 2004 06:29 To: [EMAIL PROTECTED] Subject: Re: Cool object trick Kinda cool. It's occured to me that just about everything Pythonic can be done with dicts and functions. Your Obj is just a

Re: Cool object trick

2004-12-16 Thread Jive
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

Re: Cool object trick

2004-12-12 Thread Steven Bethard
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

Cool object trick

2004-12-12 Thread dataangel
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: #--