[Tutor] subclass question

David Perlman dperlman at wisc.edu
Sun Dec 20 05:41:21 CET 2009


If I make a subclass of a built-in class, like this:

class mylist(list):
     def __init__(self):
         list.__init__(self)

Then it is valid for me to do this:

 >>> x=mylist()
 >>> x.hello=3
 >>>

But I can't do this:

 >>> y=list()
 >>> y.hello=3
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'hello'
 >>>

What is it that is special about built-in classes that prevents you  
from adding methods or, uh, whatever the generic term is for sub- 
variables?  Is there a way to make your own classes restricted like  
that?

OK thanks!


--
-dave----------------------------------------------------------------
Unfortunately, as soon as they graduate, our people return
to a world driven by a tool that is the antithesis of thinking:
PowerPoint. Make no mistake, PowerPoint is not a neutral tool —
it is actively hostile to thoughtful decision-making. It has
fundamentally changed our culture by altering the expectations
of who makes decisions, what decisions they make and how
they make them.  -Colonel T. X. Hammes, USMC



More information about the Tutor mailing list