473,769 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: attribute access and comparisons of two different objects

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) This is driving me nuts: I do not want to compare apples and peas.
I can say that they are not equal, but I cannot say that one is great
than the other (speaking not of greater taste ;-). Just ran into a
problem caused by comparing a string with a number ("1" > 10) -- I
simply forgot to convert the string to an integer. Since I cannot add
"1" + 10 which makes sense, I do not want to compare them. Any
development regarding this? Any """from __future__ import"""?

- Chris
Jul 18 '05 #1
6 1426
1) In Python 2.3 there is a new __slots__ methodology that
does what you want with class attributes. One must wonder
how everyone got by without it for so many years. I'm not
sure I understand the "overhead" issue. Some code must be
executed to determine if an attribute exists or not, why
shouldn't it be up to the programmer to write it by
overriding __setattr__ method?

2) Why don't these programming languages do what I mean
instead of what I tell them to do? ;-)

HTH,
Larry Bates
Syscon, Inc.

"Chris..." <ch************ @web.de> wrote in message
news:24******** *************** ***@posting.goo gle.com...
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) This is driving me nuts: I do not want to compare apples and peas.
I can say that they are not equal, but I cannot say that one is great
than the other (speaking not of greater taste ;-). Just ran into a
problem caused by comparing a string with a number ("1" > 10) -- I
simply forgot to convert the string to an integer. Since I cannot add
"1" + 10 which makes sense, I do not want to compare them. Any
development regarding this? Any """from __future__ import"""?

- Chris

Jul 18 '05 #2
In article <24************ **************@ posting.google. com>,
Chris... <ch************ @web.de> wrote:

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.
Don't use __slots__. Why do you think __setattr__ is a huge overhead?
2) This is driving me nuts: I do not want to compare apples and peas.
I can say that they are not equal, but I cannot say that one is great
than the other (speaking not of greater taste ;-). Just ran into a
problem caused by comparing a string with a number ("1" > 10) -- I
simply forgot to convert the string to an integer. Since I cannot add
"1" + 10 which makes sense, I do not want to compare them. Any
development regarding this? Any """from __future__ import"""?


You'll have to wait for Python 3.0 for the core to fully support this;
meanwhile, you can only force this with your own classes.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
Jul 18 '05 #3
* Chris... <ch************ @web.de> [15-06-2004 10:52]:
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.


I have a "fix" in mind, but I would like the community to comment
because I don't know if it's good practice. What about using the __slots__
attribute to prevent creation of new attributes on the fly?
class foo(object): ... __slots__ = ['attr1', 'attr2']
... a = foo()
a.attr1 = 2
a.attr2 = 3
a.attr3 = 4 Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'foo' object has no attribute 'attr3'


__slots__ is documented in
http://www.python.org/doc/current/ref/slots.html

Aloysio

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAzwSm3Z9 8a+m7958RAjI+AJ 9hZD4OuFho18EV8 3K6WWzqC69JpQCf akgp
c7WN8GxsQ8Ka+CR W2AtrsCY=
=7xiF
-----END PGP SIGNATURE-----

Jul 18 '05 #4
"Larry Bates" <lb****@swamiso ft.com> wrote in message news:<V4******* *************@c omcast.com>...
1) In Python 2.3 there is a new __slots__ methodology that
does what you want with class attributes. One must wonder
how everyone got by without it for so many years. I'm not
sure I understand the "overhead" issue. Some code must be
executed to determine if an attribute exists or not, why
shouldn't it be up to the programmer to write it by
overriding __setattr__ method?


__slots__ should never be used to restrict attribute access;
they are just a memory saving optimization; you are better off
not using it if you can. See this recipe:

http://aspn.activestate.com/ASPN/Coo.../Recipe/252158

Yes, overriding __setattr__ has a performance overhaud, so
just do not freeze your attributes! That's the Pythonic solution.

Michele Simionato
Jul 18 '05 #5

Permit me to comment on this. Restricted atrtibute access may not be a
feature of __slots__, but combined with the memory savings and run time
improvement, it is another, secondary benefit of __slots__.

Overriding __setattr__ provides restricted access but at a significant
run time cost compared to __slots__ and without the other benefits of
__slots__. My posting from May 15 shows some figures without
overloading __setattr__. (Google "group:comp.lan g.python.* __slots__ vs
__dict__").

Based on other postings in this group there seems to be a legitimate
need for limiting class extensility without incurring a signficant
performance penalty. For production Python applications the choice
between using __slots__ and overriding __setattr__ is obviously in
favor of the former.

/Jean Brouwers
ProphICy Semiconductor, Inc.
In article <95************ **************@ posting.google. com>, Michele
Simionato <mi************ ***@poste.it> wrote:
"Larry Bates" <lb****@swamiso ft.com> wrote in message
news:<V4******* *************@c omcast.com>...
1) In Python 2.3 there is a new __slots__ methodology that
does what you want with class attributes. One must wonder
how everyone got by without it for so many years. I'm not
sure I understand the "overhead" issue. Some code must be
executed to determine if an attribute exists or not, why
shouldn't it be up to the programmer to write it by
overriding __setattr__ method?


__slots__ should never be used to restrict attribute access;
they are just a memory saving optimization; you are better off
not using it if you can. See this recipe:

http://aspn.activestate.com/ASPN/Coo.../Recipe/252158

Yes, overriding __setattr__ has a performance overhaud, so
just do not freeze your attributes! That's the Pythonic solution.

Michele Simionato

Jul 18 '05 #6
aa**@pythoncraf t.com (Aahz) wrote in message news:<ca******* ***@panix3.pani x.com>...
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.


Don't use __slots__. Why do you think __setattr__ is a huge overhead?


Ok, I won't use __slots__. I was trying it anway and found out that
it doesn't satisfy my needs. I do not like the idea of implementing
__setattr__ either. For each class I have to write my own __setattr__
which has to look up a class attribute like __attributes__ = ['attr1',
'attr2']. But I have to write it for each new class, right?

- Chris
Jul 18 '05 #7

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

Similar topics

6
3639
by: Ruud de Jong | last post by:
I have the situation where I need to construct the name of a static method, and then retrieve the corresponding function from a class object. I thought I could just use __getattribute__ for this purpose. This works fine if I already have an instantiation of the class, but not when I try this on the class object directly. A bare bones example:
11
1519
by: Dietrich Epp | last post by:
Without invoking double-underscore black magic, it is possible to choose a, b, and c in Python such that: a < b b < c c < a This could cause sorting functions to malfunction. >>> class t(object):
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
11
17061
by: Rosco | last post by:
Does anyone have a good URL or info whre Oracle and Access are compared to one another in performance, security, cost etc. Before you jump on me I know Oracle is a Cadillac compared to Access the Ford Fairlane. I need this info to complete a school project. Thanks.
1
1227
by: Gürkan Demirci | last post by:
Hi, i am using the VisualStudio FormDesigner to create an asp:table. I want to populate an asp:tablecell with different controls at runtime. In the codebehind file, there is an attribute for the asp:table. I can use this to access the tablerow and tablecell objects. It would be more convenient to access a tablecell through a class-attribute. Why is VisualStudio not creating an attribute for tablerow or tablecell ?
1
1824
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the code, as there's simply too much, but here's the general gist: I have a connection object which connects to a custom back-end server of one type or another. Clients of this connection object send requests via a method (e.g....
2
2009
by: TheDrizzle | last post by:
Hey All, I am using ADF UIX, which is a Oracle Framework to develop a web application. It is based off XML and translates the XML to HTML upon execution. The problem I'm having is it is creating some anchors I need to access in JavaScript without a name or ID attribute. There is also no way in the XML i can assign these objects a name or ID. For example, I have this <dateField> xml tag I create. This renders on the HTML page as a input...
18
6783
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work, until I subclassed a class using this technique and from there on things started screwing up. I finally tracked it down to self.__class__.attribute! What was happening is that the child classes each over-rode the class attribute at their level,...
8
1432
by: chamalulu | last post by:
Hello. I think I'm aware of how attribute access is resolved in python. When referencing a class instance attribute which is not defined in the scope of the instance, Python looks for a class attribute with the same name. (For assignment or deletion this is not the case, thankfully.) I've been trying to understand why? What is the reason behind, or practical purpose of, this design decision? Anyone, please enlighten me.
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
10049
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.