my own type in C, sequence protocol

T

Torsten Mohr

Hi,

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?

Do i need to implement more functions for the sequence
protocol?


Best regards,
Torsten.
 
D

Donn Cave

Torsten Mohr said:
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, (e-mail address removed)
 
B

Brandon Craig Rhodes

Donn Cave said:
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])

Note, however, that this requires Python to create a list with the
values 1, 2, and 3 in it; then pass that value to your init function;
and finally discard the list when the initializer has completed.
Perhaps more efficient (maybe you could test both to compare the
expense?) would be for your initialization function to accept the new
sequence members as arguments themselves:

m = myType(1, 2, 3)

This way a list does not have to be created and destroyed, with all
the associated computation, each time you initialize a myType.
 
T

Torsten Mohr

Hi,
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++.

thanks for that explanation. From the error messages i
got i already thought of something like this. But still
i'm confused, what is the advantage to implement the
sequence protocol for my type then?

The type that i implemented is a "CAN message", don't know
if this means something to you, it is a communication message
with 0 to 8 bytes content. To access this content i thought
there would be an advantage in implementing the sequence
protocol. What is the advantage then?


Best regards,
Torsten.
 
A

Alex Martelli

Brandon Craig Rhodes said:
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])

Note, however, that this requires Python to create a list with the
values 1, 2, and 3 in it; then pass that value to your init function;
and finally discard the list when the initializer has completed.

Yeah, so?
Perhaps more efficient (maybe you could test both to compare the
expense?) would be for your initialization function to accept the new
sequence members as arguments themselves:

m = myType(1, 2, 3)

This way a list does not have to be created and destroyed, with all
the associated computation, each time you initialize a myType.

Yeah, but a tuple (of arguments) still has to be.

Sure, there IS a performance difference, but it's very small...:

kallisti:~ alex$ python cb/timeit.py -s'
class mt:
def __init__(self, seq): self.seq=list(seq)
' 'mt([ 1,2,3 ])'
100000 loops, best of 3: 9.52 usec per loop

kallisti:~ alex$ python cb/timeit.py -s'
class mt:
def __init__(self, *seq): self.seq=list(seq)
' 'mt(1,2,3)'
100000 loops, best of 3: 8.38 usec per loop
kallisti:~ alex$

You're not going to create enough instances to care about that
microsecond's difference (and that's on a modest 800 MHz iBook G4 cheap
laptop a year old...). To me, it seems very much like a case where
design decisions should be based on clarity and simplicity, not on tiny
performance advantages...!


Alex
 
H

Heiko Wundram

Am Donnerstag, 9. September 2004 22:21 schrieb Torsten Mohr:
The type that i implemented is a "CAN message", don't know
if this means something to you, it is a communication message
with 0 to 8 bytes content. To access this content i thought
there would be an advantage in implementing the sequence
protocol. What is the advantage then?

I don't know what a CAN message is, but implementing the sequence protocol
certainly has its features.

What you can do to reassign the whole of m:

m[:] = [1,2,3]

Now, if you implemented the sequence protocol correctly (meaning it can deal
with slices, this will reassign the whole of m. Lists support slice
assignment:
x = [1,2,3]
id(x) -1477186996
x[:] = [4,5,6]
x [4, 5, 6]
id(x)
-1477186996

(see how x is not rebound?)

Well, anyway, maybe you can just paste some example code (a more thorough
explanation would also be okay) for us to see, so that we know what you're
trying to do, and then suggest a proper idiom to use... Currently I can't
really grasp what you need the sequence protocol for...

Heiko.
 
G

Grant Edwards

thanks for that explanation. From the error messages i got i
already thought of something like this. But still i'm
confused, what is the advantage to implement the sequence
protocol for my type then?

The type that i implemented is a "CAN message", don't know if
this means something to you, it is a communication message
with 0 to 8 bytes content. To access this content i thought
there would be an advantage in implementing the sequence
protocol. What is the advantage then?

Good question.

[I just use lists of integers for my CAN messages.]

If you want to iterate over your object, you probably want to
support the sequence protocol.
 
G

Grant Edwards

The type that i implemented is a "CAN message", don't know if
this means something to you, it is a communication message
with 0 to 8 bytes content. To access this content i thought
there would be an advantage in implementing the sequence
protocol. What is the advantage then?

Good question.

[I just use lists of integers for my CAN messages.]

If you want to iterate over your object, you probably want to
support the sequence protocol.

Ignore that. I was conflating the sequence protocol and the
iterator protocol.
 
A

Alex Martelli

Grant Edwards said:
If you want to iterate over your object, you probably want to
support the sequence protocol.

In general, supplying a suitable __iter__ method is simpler and more
suitable than implementing the sequence protocol, if your purpose is to
allow iteration on an object.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top