There is a syntactic sugar for item access in
dictionaries and sequences:
o[e] = v <-> o.__setitem__(e, v)
o[e] <-> o.__getitem__(e)
where e is an expression.
There is no similar way for set/get attribute for objects.
If e is a given name, then
o.e = v <-> o.__setattr__(e, v)
o.e <-> o.__getattr__(e)
Anybody thought about this issue?
We could write something like this:
o.[e] = v
o.[e]
or
o.(e) = v
o.(e)
in this case.
For example:
setattr(self, method_name, getattr(self.metadata,
method_name)) -->
self.(method_name) = self.metadata.(method_name)
setattr(command_obj, neg_opt[option], not
strtobool(value)) -->
command_obj.(neg_opt[option]) = not strtobool(value)
setattr(self, '%s_open' % type,
lambda r, proxy=url, type=type,
meth=self.proxy_open:
meth(r, proxy, type)) -->
self.('%s_open' % type) =
lambda r, proxy=url, type=type,
meth=self.proxy_open: meth(r, proxy, type) 9 1705
úÁÕÒ ûÉÂÚÕÈÏ× wrote: There is a syntactic sugar for item access in dictionaries and sequences:
o[e] = v <-> o.__setitem__(e, v) o[e] <-> o.__getitem__(e)
where e is an expression.
There is no similar way for set/get attribute for objects. If e is a given name, then
o.e = v <-> o.__setattr__(e, v) o.e <-> o.__getattr__(e)
Anybody thought about this issue?
Perhaps not, but now that you've pointed it out they've taken the time
machine back and fixed the problem before it arose: class C:
.... def __setattr__(self, e, v):
.... print 'setting %s to %s' % (e, v)
.... self.__dict__[e] = v
.... o = C() v = 'mystring' o.e = v
setting e to mystring o.e
'mystring'
-Peter
Peter Hansen wrote: úÁÕÒ ûÉÂÚÕÈÏ× wrote:
There is a syntactic sugar for item access in dictionaries and sequences:
o[e] = v <-> o.__setitem__(e, v) o[e] <-> o.__getitem__(e)
where e is an expression.
There is no similar way for set/get attribute for objects. If e is a given name, then
o.e = v <-> o.__setattr__(e, v) o.e <-> o.__getattr__(e)
Anybody thought about this issue?
Perhaps not, but now that you've pointed it out they've taken the time machine back and fixed the problem before it arose:
>>> class C: ... def __setattr__(self, e, v): ... print 'setting %s to %s' % (e, v) ... self.__dict__[e] = v ... >>> o = C() >>> v = 'mystring' >>> o.e = v setting e to mystring >>> o.e 'mystring' >>>
I think he means something like this:
e = 'i_am_an_attribute'
o.(e) = 10
o.i_am_an_attribute == 10
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Robert Kern wrote: úÁÕÒ ûÉÂÚÕÈÏ× wrote:
There is a syntactic sugar for item access in dictionaries and sequences:
o[e] = v <-> o.__setitem__(e, v) o[e] <-> o.__getitem__(e)
where e is an expression.
There is no similar way for set/get attribute for objects. If e is a given name, then o.e = v <-> o.__setattr__(e, v) o.e <-> o.__getattr__(e)
Anybody thought about this issue?
I think he means something like this: e = 'i_am_an_attribute' o.(e) = 10 o.i_am_an_attribute == 10
I always use the getattr() and setattr() built-ins which could be
considered syntactic sugar when compared to the alternatives above.
But that's all the syntactic sugar you need--any more will give you
cancer of the semicolon.
--
Michael Hoffman
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Peter Hansen wrote: Заур Шибзухов wrote:
There is a syntactic sugar for item access in dictionaries and sequences:
o[e] = v <-> o.__setitem__(e, v) o[e] <-> o.__getitem__(e)
where e is an expression.
There is no similar way for set/get attribute for objects. If e is a given name, then o.e = v <-> o.__setattr__(e, v) o.e <-> o.__getattr__(e)
Anybody thought about this issue?
Perhaps not, but now that you've pointed it out they've taken the time machine back and fixed the problem before it arose:
Maybe funny, but a bit too cocky for my taste. Robert kern is propably
right about what he really meant so don't be too hasty in the future,
right?). Looking at his code example I got the picture that he's of the
kind that could come up with something useful. So either he's
right(which I think is the case), or it's just the kind of silly mistake
all of us sometimes make. I sure think some one should look in to this
suggestion. class C: ... def __setattr__(self, e, v): ... print 'setting %s to %s' % (e, v) ... self.__dict__[e] = v ... o = C() v = 'mystring' o.e = v setting e to mystring o.e 'mystring'
-Peter
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFCweYyctNFyQJObrsRAhGtAJwJXlhQ9i1PIQKj1fus6G Iq7mfDVgCeJBRw
vq6yJrozRTUSTu+p8akVbVw=
=k4EW
-----END PGP SIGNATURE-----
On Tuesday 28 June 2005 07:07 pm, Elmo Mäntynen wrote: Peter Hansen wrote: Заур Шибзухов wrote: There is a syntactic sugar for item access in dictionaries and sequences: o[e] = v <-> o.__setitem__(e, v) o[e] <-> o.__getitem__(e)
where e is an expression.
There is no similar way for set/get attribute for objects. If e is a given name, then o.e = v <-> o.__setattr__(e, v) o.e <-> o.__getattr__(e)
Anybody thought about this issue?
I'm pretty sure it's been discussed. Javascript unifies the concepts of
dictionary access and object attribute access into the single concept
of "associational array" (there are some limitations to the Javascript
version, so they aren't really equivalent, but for simple cases they
often are).
In Javascript, the expression
a['b']
means the same thing as
a.b
Perhaps not, but now that you've pointed it out they've taken the time machine back and fixed the problem before it arose:
Maybe funny, but a bit too cocky for my taste. Robert kern is propably right about what he really meant so don't be too hasty in the future, right?). Looking at his code example I got the picture that he's of the kind that could come up with something useful. So either he's right(which I think is the case), or it's just the kind of silly mistake all of us sometimes make. I sure think some one should look in to this suggestion.
Could be, but I think the absence of "sugar" in this situation is
at least semi-intentional. We already have dictionary access for
when this type of situation is needed. And if you really want an
object that permits either type of access --- that is, for which
dictionary access and attribute access are the same --- it is not
difficult to create one.
OTOH, if it were automatic, it would tend to erase any distinction
between the two concepts. Stylistically, dictionaries are the "right"
thing to use when the elements are going to be very fluid, whereas
objects are expected to have more or less fixed attribute and method
interfaces. Making "access to attribute by string name" more
difficult than "access to dictionary value by string key" is one way
to encourage the distinction.
And, really, when you do need it, __getattr__ / __setattr__ aren't
really *that* difficult to use.
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
Elmo Mäntynen wrote: Peter Hansen wrote:Заур Шибзухов wrote:Anybody thought about this issue?
Perhaps not, but now that you've pointed it out they've taken the time machine back and fixed the problem before it arose:
Maybe funny, but a bit too cocky for my taste. Robert kern is propably right about what he really meant so don't be too hasty in the future, right?).
Elmo, it's probably neither cocky nor funny, but before you pass
judgment you should Google this group for "time machine" and read
awhile. (I was merely attempting to parrot a traditional response that
is given around here when someone asks for something which is already
present in the language.)
And whether I misinterpreted the (ambiguous) question or not, my
response provides the required information to put together a solution to
the OP's question. It would just require one extra level of
indirection, so to speak, to do what Robert suggests he might have wanted.
(Uncocky enough for you this time?)
-Peter
Peter Hansen wrote: Elmo Mäntynen wrote:
Maybe funny, but a bit too cocky for my taste. Robert kern is propably right about what he really meant so don't be too hasty in the future, right?).
Elmo, it's probably neither cocky nor funny, but before you pass judgment you should Google this group for "time machine" and read awhile. (I was merely attempting to parrot a traditional response that is given around here when someone asks for something which is already present in the language.)
And whether I misinterpreted the (ambiguous) question or not, my response provides the required information to put together a solution to the OP's question. It would just require one extra level of indirection, so to speak, to do what Robert suggests he might have wanted.
I didn't understand the original question either until Robert Kern
guessed what the OP was talking about.
--
Michael Hoffman
> e = 'i_am_an_attribute' o.(e) = 10 o.i_am_an_attribute == 10
Yes I mean this thing: to write
o.(e) = 10
or
o.[e] = 10 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christopher T King |
last post by:
Okay, so this is really two requests in one, and they're both kinda
outlandish, but I'm gonna post them nonetheless:
I've always thought xrange() to be ugly; it looks to be a lot of typing
just...
|
by: Philip Smith |
last post by:
I've read with interest the continuing debate about 'lambda' and its place
in Python.
Just to say that personally I think its an elegant and useful construct for
many types of programming task...
|
by: Neil Zanella |
last post by:
Hello,
It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Basically, what more do they have...
|
by: Bas |
last post by:
Hi group,
just out of curiosity, is there a list of all the syntactic sugar that
is used in python? If there isn't such a list, could it be put on a
wiki somewhere? The bit of sugar that I do...
|
by: collection60 |
last post by:
Hi people,
I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating are
basically just pointers. So if it's a pointer...
| |
by: Sam Kong |
last post by:
Hi,
While discussing C#'s using statement, a guy and I had an argument.
In C# spec (15.13), there's an explanation like the following.
using (R r1 = new R()) {
r1.F();
}
is precisely...
|
by: 7stud |
last post by:
"When you bind (on either a class or an instance) an attribute whose
name is not special...you affect only the __dict__ entry for the
attribute(in the class or instance, respectively)."
In light...
|
by: Helmut Jarausch |
last post by:
Hi,
are decorators more than just syntactic sugar in python 2.x and what
about python 3k ?
How can I find out the predefined decorators?
Many thanks for your help,
Helmut Jarausch
|
by: MonkeeSage |
last post by:
Proposal:
When an attribute lookup fails for an object, check the top-level
(and local scope?) for a corresponding function or attribute and apply
it as the called attribute if found, drop...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |