[Tutor] subtyping builtin type

spir denis.spir at gmail.com
Tue Dec 31 15:35:55 CET 2013


Hello,

I don't remember exactly how to do that. As an example:

class Source (str):
     __slots__ = ['i', 'n']
     def __init__ (self, string):
         self.i = 0                  # current matching index in source
         self.n = len(string)        # number of ucodes (Unicode code points)
         #~ str.__init__(self, string)

I thought I needed to call str's __init__, as in the line comented out, but (1) 
python refuses with a TypeError (2) all seems to work fine (meaning, the string 
is well stored, *implicitely*). Am I missing some point, or is this the way to 
do? How does it work? I particular, how does python know which param to take as 
source string? (There could be other params to __init__.)

Thank you,
Denis


More information about the Tutor mailing list