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
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
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
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
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
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
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
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
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. ;)
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
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
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
[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
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
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
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
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!"
: [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
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
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
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
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:
#--
22 matches
Mail list logo