deleting a method

Ben Finney bignose+hates-spam at benfinney.id.au
Sat Jan 3 23:41:45 CET 2009


MRAB <google at mrabarnett.plus.com> writes:

> Filip Gruszczyński wrote:
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > AttributeError: A instance has no attribute 'foo'
> >
> > Why is it so and how may still delete it?
> >
> 'a' is an instance of class A. You're trying to delete 'foo' from
> the instance.

To answer the second question: since ‘foo’ is an attribute of the
class ‘A’, you can delete the attribute from the class.

    >>> class A(object):
    ...     def foo(self):
    ...         pass
    ... 
    >>> a = A()
    >>> 'foo' in dir(a)
    True
    >>> del A.foo
    >>> 'foo' in dir(a)
    False

-- 
 \       “If you don't know what your program is supposed to do, you'd |
  `\                 better not start writing it.” —Edsger W. Dijkstra |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list