my own type in C, sequence protocol

Donn Cave donn at u.washington.edu
Thu Sep 9 15:48:40 EDT 2004


In article <chq9eg$c2i$1 at schleim.qwe.de>,
 Torsten Mohr <tmohr at s.netic.de> wrote:

> i created a new type and implemented the sequence protocol
> for it, or to be more precise the functions to get the
> sequence length, to read a value and to assign a value
> to an item.
> 
> Now i thought i could assign values to my type like
> this:
> 
> m = myType();
> m = [ 1, 2, 3];
> 
> I have to admit that i'm not really experienced in python.
> 
> Now that i have implemented the sequence protocol, what
> can i do with it, can't i assign values like above somehow?

Nope.  The expression [1, 2, 3] evaluates to a list,
as in listobject.  m = [1, 2, 3] binds the name "m"
to this object.  Its prior binding to your myType()
instance doesn't matter.  If you're new to Python, it's
lucky you ran into this so early, because this business
about names and objects is an important difference between
Python and, for example, C++.

You might want your type's init function to accept a
sequence, to copy for an initial value, as in

 m = myType([1, 2, 3])

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list