473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Setting an attribute without calling __setattr__()

OK, I'm sure the answer is staring me right in the face--whether that answer
be "you can't do that" or "here's the really easy way--but I am stuck. I'm
writing an object to proxy both lists (subscriptable iterables, really) and
dicts.

My init lookslike this:

def __init__(self, obj=None):
if type(obj).__name__ in 'list|tuple|set|frozenset':
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))
elif type(obj) == dict:
self.me = {}
for k,v in obj.items():
self.me[k] = ObjectProxy(v)

and I have a __setattr__ defined like so:

def __setattr__(self, name, value):
self.me[name] = ObjectProxy(value)

You can probably see the problem.

While doing an init, self.me = {} or self.me = [] calls __setattr__, which
then ends up in an infinite loop, and even it it succeeded

self.me['me'] = {}

is not what I wanted in the first place.

Is there a way to define self.me without it firing __setattr__?

If not, it's not a huge deal, as having this class read-only for now won't
be a problem, but I was just trying to make it read/write.

Thanks!

j

Jun 27 '08 #1
12 1190
Joshua Kugler <jk*****@bigfoot.comwrites:
self.me = []
self.me = {}
Use "object.__setattr__(self, 'me') = []" and likewise for {}.
Jun 27 '08 #2
On Apr 26, 7:01 am, Joshua Kugler <jkug...@bigfoot.comwrote:
OK, I'm sure the answer is staring me right in the face--whether that answer
be "you can't do that" or "here's the really easy way--but I am stuck. I'm
writing an object to proxy both lists (subscriptable iterables, really) and
dicts.

My init lookslike this:

def __init__(self, obj=None):
if type(obj).__name__ in 'list|tuple|set|frozenset':
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))
elif type(obj) == dict:
self.me = {}
for k,v in obj.items():
self.me[k] = ObjectProxy(v)

and I have a __setattr__ defined like so:

def __setattr__(self, name, value):
self.me[name] = ObjectProxy(value)

You can probably see the problem.

While doing an init, self.me = {} or self.me = [] calls __setattr__, which
then ends up in an infinite loop, and even it it succeeded

self.me['me'] = {}

is not what I wanted in the first place.

Is there a way to define self.me without it firing __setattr__?
Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.
Jun 27 '08 #3
Hrvoje Niksic <hn*****@xemacs.orgwrites:
Joshua Kugler <jk*****@bigfoot.comwrites:
> self.me = []
self.me = {}

Use "object.__setattr__(self, 'me') = []" and likewise for {}.
Oops, that should of course be "object.__setattr__(self, 'me', [])".
Jun 27 '08 #4
John Machin wrote:
>Is there a way to define self.me without it firing __setattr__?
Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.
Like I said in my original post, it was probably staring me right in the
face. I had read through a bit of the documentation on special methods,
but for some reason I missed that part.

Thanks to all for your responses!

j

Jun 27 '08 #5
On Apr 25, 5:01 pm, Joshua Kugler <jkug...@bigfoot.comwrote:
My init lookslike this:

def __init__(self, obj=None):
if type(obj).__name__ in 'list|tuple|set|frozenset':
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))
elif type(obj) == dict:
self.me = {}
for k,v in obj.items():
self.me[k] = ObjectProxy(v)
As an aside, unrelated to your question, Python encourages "duck
typing" instead of exact type matching. Unless you have a good reason
to restrict obj to one of the 5 types you hardcoded (a rather rare
need), it is more flexible to write it as:

def __init__(self, obj=None):
if hasattr(obj, 'items'):
# assume obj is a mapping type instance
self.me = dict((k,ObjectProxy(v)) for k,v in obj.items())
else:
try: # check if obj is an iterable instance
self.me = map(ObjectProxy, obj)
except TypeError:
# handle other cases here
# self.me = ...
A downside of this flexibility is that it may be more liberal than it
should. For instance, if obj just happens to have an 'items()' method
but it's not really a mapping type, the assumption is violated. Python
3 deals with such potential ambiguities by introducing Abstract Base
Classes (ABCs) [1] that allow a class to make explicit its semantics.
So in Py3K the hasattr() test above would rather be written as
"isinstance(obj, Mapping)", where Mapping is the ABC that represents
(read-only) mappings.

A more difficult problem is that even if a class derives from some
ABC, you may not always want to treat its instances as such. The
typical gotcha is that strings are iterable, but in many (most?)
applications they are to be treated as atomic values, not as sequences
of characters. So in the example above if obj is a string, self.me
will be a list of ObjectProxy instances, one per character; probably
not what you intend. Of course we can check for "isinstance(obj,str)"
but then we're back at explicit type checking. There is no general way
to express something lke "atomic value that also happens to be
iterable (but pretend it's not)" because it's inherently domain-
dependent.

George

[1] http://www.python.org/dev/peps/pep-3119/
Jun 27 '08 #6
Joshua Kugler <jk*****@bigfoot.comwrites:

[...]
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))
Note that is could be spelt:

self.me = map(ObjectProxy, v)

--
Arnaud
Jun 27 '08 #7


Hrvoje Niksic wrote:
Hrvoje Niksic <hn*****@xemacs.orgwrites:
Joshua Kugler <jk*****@bigfoot.comwrites:
self.me = []
self.me = {}
Use "object.__setattr__(self, 'me') = []" and likewise for {}.

Oops, that should of course be "object.__setattr__(self, 'me', [])".
Jun 27 '08 #8
Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.
if you are simply going to answer rtfm - might as well kept it to
yourself.
Jun 27 '08 #9
In article <m2************@googlemail.com>,
Arnaud Delobelle <ar*****@googlemail.comwrote:
>Joshua Kugler <jk*****@bigfoot.comwrites:
>>
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))

Note that is could be spelt:

self.me = map(ObjectProxy, v)
It could also be spelt:

self.me = [ObjectProxy(v) for v in obj]

which is my preferred spelling....
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
Jun 27 '08 #10
On Sat, 26 Apr 2008 08:28:38 -0700, animalMutha wrote:
>Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.

if you are simply going to answer rtfm - might as well kept it to
yourself.
Yes, but if you are telling where exactly to find the wanted information
in the documentation, like John did, you are teaching the OP how to fish.
Which is a good thing. Much more helpful than your remark anyway. You
might as well have kept it to yourself. :-þ

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #11
aa**@pythoncraft.com (Aahz) writes:
In article <m2************@googlemail.com>,
Arnaud Delobelle <ar*****@googlemail.comwrote:
>>Joshua Kugler <jk*****@bigfoot.comwrites:
>>>
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))

Note that is could be spelt:

self.me = map(ObjectProxy, v)
^-- I meant obj!
>
It could also be spelt:

self.me = [ObjectProxy(v) for v in obj]

which is my preferred spelling....
I was waiting patiently for this reply... And your preferred spelling
is py3k-proof as well, of course.

I don't write map(lambda x: x+1, L) or map(itemgetter('x'), L) but I
like to use it when the first argument is a named function,
e.g. map(str, list_of_ints).

--
Arnaud
Jun 27 '08 #12
animalMutha wrote:
>Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.

if you are simply going to answer rtfm - might as well kept it to
yourself.
For what it's worth, I (the original poster) am glad he answered that way.
It showed me the section and paragraph I had overlooked when reading
through the docs the first time.

j

Jun 27 '08 #13

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

Similar topics

4
by: Maarten van Reeuwijk | last post by:
Hello, Maybe I was a little too detailed in my previous post . I can boil down my problem to this: say I have a class A that I encapsulate with a class Proxy. Now I just want to override and add...
6
by: Chris... | last post by:
Two simple questions regarding future of Python: 1) Is there already a "fix" to avoid writing to an attribute that isn't defined yet? I remember this being an often discussed problem, but...
1
by: Thomas Heller | last post by:
I have a subclassable type implemented in C, which has a 'value' attribute implemented in the tp_getset slot. The type is named c_long. The value attribute accepts and returns integers. Now I...
3
by: Christian Dieterich | last post by:
Hi, I need to create many instances of a class D that inherits from a class B. Since the constructor of B is expensive I'd like to execute it only if it's really unavoidable. Below is an example...
9
by: úÁÕÒ ûÉÂÚÕÈÏ× | last post by:
There is a syntactic sugar for item access in dictionaries and sequences: o = v <-> o.__setitem__(e, v) o <-> o.__getitem__(e) where e is an expression. There is no similar way for set/get...
5
by: Joel Andres Granados | last post by:
Hi list: I have googled quite a bit on this matter and I can't seem to find what I need (I think Im just looking where I'm not suppose to :). I'm working with code that is not of my authorship...
0
by: Terry Reedy | last post by:
"Joshua Kugler" <jkugler@bigfoot.comwrote in message news:futgrq$ih6$1@ger.gmane.org... | OK, I'm sure the answer is staring me right in the face--whether that answer | be "you can't do that" or...
8
by: Ken Starks | last post by:
I have a class with an attribute called 'gridsize' and I want a derived class to force and keep it at 0.8 (representing 8mm). Is this a correct, or the most pythonic approach? ...
2
by: Jan Schilleman | last post by:
Hi all, I am trying to redefine __setattr__. The base class is in a library (actually, it is win32com.client.DispatchBaseClass) and I do not want to touch it. My problem is exemplified below....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.