Is it possible to have instance variables in subclasses of builtins?

Robert Brewer fumanchu at amor.org
Tue Jun 15 18:38:48 EDT 2004


Kenneth McDonald wrote:
> I've recently used subclasses of the builtin str class to good effect.
> However, I've been unable to do the following:
> 
> 1) Call the constructor with a number of arguments other than
> the number of arguments taken by the 'str' constructor.
> 
> 2) Create and use instance variables (eg. 'self.x=1') in
> the same way that I can in a 'normal' class.

Since str is an immutable, you need to override __new__ as well as
__init__.

http://www.python.org/2.2.3/descrintro.html


FuManChu




More information about the Python-list mailing list