Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Q: attribute access and comparisons of two different objects

Reply
Thread Tools

Q: attribute access and comparisons of two different objects

 
 
Chris...
Guest
Posts: n/a
 
      06-15-2004
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
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      06-15-2004
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..." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
Aloysio Figueiredo
Guest
Posts: n/a
 
      06-15-2004
* Chris... <(E-Mail Removed)> [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)

iD8DBQFAzwSm3Z98a+m7958RAjI+AJ9hZD4OuFho18EV83K6WW zqC69JpQCfakgp
c7WN8GxsQ8Ka+CRW2AtrsCY=
=7xiF
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      06-15-2004
In article <(E-Mail Removed) >,
Chris... <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      06-16-2004
"Larry Bates" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Jean Brouwers
Guest
Posts: n/a
 
      06-16-2004

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.lang.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 <(E-Mail Removed) >, Michele
Simionato <(E-Mail Removed)> wrote:

> "Larry Bates" <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)>...
> > 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

 
Reply With Quote
 
Chris...
Guest
Posts: n/a
 
      06-16-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Aahz) wrote in message news:<cangqq$33i$(E-Mail Removed)>...
> >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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Published performance comparisons between two processors Henry Ross Computer Support 1 02-18-2008 10:37 PM
Rebuild list of objects with redundancies on objects' attribute Nico Grubert Python 1 01-10-2008 11:29 AM
JNDI: Delete only one attribute when there are several different values for the same attribute bsporb@gmail.com Java 3 05-02-2007 05:41 AM
Speed comparisons - LWP and database access trashman.horlicks@btinternet.com Perl Misc 3 01-24-2007 08:17 PM
I thought that array bounds checking needed two comparisons; however, ... Casey Hawthorne Java 21 06-05-2004 08:04 PM



Advertisments