473,503 Members | 5,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set/Get attribute syntatic sugar

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)

Jul 19 '05 #1
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
Jul 19 '05 #2
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

Jul 19 '05 #3
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
Jul 19 '05 #4
-----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-----
Jul 19 '05 #5
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

Jul 19 '05 #6
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
Jul 19 '05 #7
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
Jul 19 '05 #8
Yes, I mean this thing.

Jul 19 '05 #9
> 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

Jul 19 '05 #10

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

Similar topics

0
1408
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...
7
1559
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...
13
5645
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...
4
8878
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...
8
1295
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...
13
2494
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...
3
1557
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...
11
2079
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
4
1416
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...
0
7067
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...
1
6975
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...
0
7449
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...
0
5562
agi2029
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,...
1
4992
isladogs
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...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
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...

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.