deriving from array.array
Jerry Hill
malaclypse2 at gmail.com
Tue Jan 26 14:53:02 EST 2010
On Tue, Jan 26, 2010 at 2:28 PM, Torsten Mohr <tmohr at s.netic.de> wrote:
> Is there a way to find out what i need to call? I haven't found much in
> the documentation. From writing C extensions i knew about the "new" entry
> in the PyTypeObject struct but it seems there's more behind it.
> In docs.python.org i did not find much, is there an URL where i can read
> more?
>
http://docs.python.org/reference/datamodel.html#basic-customization talks
about __new__() and __init__(). The basic rule of thumb is that when you
inherit from an immutable type, you need to override __new__, and when you
inherit from a mutable type you need to override __init__. I don't think
that rule of thumb applies here, though, since an array.array is mutable.
The actual rule is that you need to override __new__ if the class you're
inheriting from overrides __new__. I don't think there's a straightforward
way to know that ahead of time unless you look at the source code for the
class. Either that, or you just see that you get a TypeError when
initializing your object and realize it means you need to override the
constructor.
It would be nice if the TypeError was a little more explicit about what was
going on, though. Here's what I get in IDLE for a class similar to yours:
>>> import array
>>> class my_a(array.array):
def __init__(self, a, b):
array.array.__init__(self, 'B')
>>> a = my_a(1, 2)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
a = my_a(1, 2)
TypeError: array() argument 1 must be char, not int
It would be nice if the traceback mentioned something about array.__new__()
instead of just array().
--
Jerry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100126/eacf7657/attachment-0001.html>
More information about the Python-list
mailing list