473,545 Members | 2,717 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.me tadata,
method_name)) -->
self.(method_na me) = self.metadata.( method_name)

setattr(command _obj, neg_opt[option], not
strtobool(value )) -->
command_obj.(ne g_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 1709
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__(sel f, 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
dictionarie s 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__(sel f, 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_attrib ute'
o.(e) = 10
o.i_am_an_attri bute == 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_attrib ute'
o.(e) = 10
o.i_am_an_attri bute == 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__(sel f, 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

iD8DBQFCweYyctN FyQJObrsRAhGtAJ wJXlhQ9i1PIQKj1 fus6GIq7mfDVgCe JBRw
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 "associatio nal 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 anansispacework s.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_attrib ute'
o.(e) = 10
o.i_am_an_attri bute == 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 to get an efficient loop over some numbers. Since it uses the same 'parameters' as the slice operator, I was thinking it would be neat if the slice...
7
1562
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 (particularly number theory/artificial intelligence/genetic algorithms) I can't think why anyone would be proposing to do away with it. Sometimes...
13
5649
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 to offer? Thank you for your replies, Best Regards,
4
8885
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 know have helped me a lot in understanding the inner workings of python. To give a few examples (might not be totally correct): x ->...
8
1301
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 to a Fred or a pointer to a long, it makes no difference. So, I want the exact same code, to be running on different types. So even if I used...
13
2500
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 equivalent to
3
1564
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 of that statement, how would one explain the output of this code: class Test(object): x =
11
2083
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
1419
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 through to the exception otherwise. This is just syntactic sugar. Example:
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5359
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1919
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 we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.