473,769 Members | 7,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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).__nam e__ 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__(sel f, name, value):
self.me[name] = ObjectProxy(val ue)

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
12 1233
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**@pythoncraf t.com (Aahz) writes:
In article <m2************ @googlemail.com >,
Arnaud Delobelle <ar*****@google mail.comwrote:
>>Joshua Kugler <jk*****@bigfoo t.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
1869
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 some functionality (see my other post why). All functionality not defined in the Proxy class should be delegated (I can't use inheritance, see other post). It should be possible to achieve this using Python's great introspection possibilities,...
6
1426
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 didn't see any changes. The only way I can think of is overriding __setattr__, but this is huge overhead. While I like the idea of being able to add new attributes on the fly, in great projects I'd like to restrict some classes not to do so. 2)...
1
2113
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 want to derive a subclass 'BOOL' (in Python) from it, where the 'value' attribute should accept and return bool instances: from ctypes import c_long class BOOL(c_long):
3
3137
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 and two workarounds, but I feel they are not really good solutions. Does somebody have any ideas how to inherit the data attributes and the methods of a class without calling it's constructor over and over again? Thank,
9
1720
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 attribute for objects. If e is a given name, then
5
1331
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 and there is a class attribute that is changes by directly referencing it (object.attr = value) instead of using a getter/setter (object.setAttr(Value) ) function. The thing is that I have no idea when the change occurs and I would REALLY like...
0
743
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 "here's the really easy way--but I am stuck. I'm | writing an object to proxy both lists (subscriptable iterables, really) and | dicts. |
8
1204
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? #################### def __getattr__(self,attrname): if attrname == 'gridsize': return 0.8
2
1438
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. To my surprise, __setattr__ and __str__ behave differently; I can redefine __str__ and the inherited __str__ is still the redefined one. But redefining __setattr__ on the base class does not get inherited. In Base.__dict__ the __setattr__ is the...
0
9589
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3965
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
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.