del(instance.attr) -- no attribute 
Author Message
 del(instance.attr) -- no attribute

Is this a bug?

Quote:
>>> class C:

...  a = 1
...
Quote:
>>> c = C()
>>> c.a
1
>>> del(c.a)

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: C instance has no attribute 'a'

Quote:
>>> hasattr(c, 'a')
1

I can see that maybe del(c.a) should not be allowed to delete
the attribute, but I think the error message is misleading.


Fri, 18 Feb 2005 05:19:14 GMT  
 del(instance.attr) -- no attribute


Quote:
> Is this a bug?

no

Quote:
>>>> class C:
> ...  a = 1
> ...
>>>> c = C()
>>>> c.a
> 1
>>>> del(c.a)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: C instance has no attribute 'a'
>>>> hasattr(c, 'a')
> 1

yes, but

Quote:
>>> del(C.a)

works

Quote:
> I can see that maybe del(c.a) should not be allowed to delete
> the attribute, but I think the error message is misleading.

but the message is true. the instance has no attribute named "a", it's an
attribute of the class "C". maybe "hasattr" is missleading to you, because
it's showing class and instace attributes, including those of base classes.
but deleting has to occour in the correct spot.

chris

--



Fri, 18 Feb 2005 05:32:58 GMT  
 del(instance.attr) -- no attribute

Quote:

> Is this a bug?

> >>> class C:
> ...  a = 1
> ...
> >>> c = C()
> >>> c.a
> 1
> >>> del(c.a)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: C instance has no attribute 'a'
> >>> hasattr(c, 'a')
> 1

> I can see that maybe del(c.a) should not be allowed to delete
> the attribute, but I think the error message is misleading.

Well, to its credit, it _does_ say that the _instance_ doesn't have the
attribute, which is precisely what the problem is.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



Fri, 18 Feb 2005 05:43:11 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Class attribute or instance attribute?

2. TypeError: attribute-less object (assign or del)

3. Types (was: Attribute vs Instance Variable)

4. Viewing instance attributes in IDLE's debugger

5. unintentionaly shared attributes in two different instances

6. Using cPickle, pickle or marshal to preserve attribute values of a class instance

7. storing instance attributes to a database

8. Querying an instance's attributes

9. Visibility of CLASS and INSTANCE attributes

10. bug when attribute name same as instance?

11. determining instance attributes

12. Variable interpolation or indirection for instance attributes

 

 
Powered by phpBB® Forum Software