Connecting Tech Pros Worldwide Forums | Help | Site Map

my own type in C, sequence protocol

Torsten Mohr
Guest
 
Posts: n/a
#1: Jul 18 '05
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.


Donn Cave
Guest
 
Posts: n/a
#2: Jul 18 '05

re: my own type in C, sequence protocol


In article <chq9eg$c2i$1@schleim.qwe.de>,
Torsten Mohr <tmohr@s.netic.de> wrote:
[color=blue]
> 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?[/color]

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@u.washington.edu
Brandon Craig Rhodes
Guest
 
Posts: n/a
#3: Jul 18 '05

re: my own type in C, sequence protocol


Donn Cave <donn@u.washington.edu> writes:
[color=blue]
> In article <chq9eg$c2i$1@schleim.qwe.de>,[/color]
[color=blue]
> 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])[/color]

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.

--
Brandon Craig Rhodes brandon@rhodesmill.org http://rhodesmill.org/brandon
Alex Martelli
Guest
 
Posts: n/a
#4: Jul 18 '05

re: my own type in C, sequence protocol


Brandon Craig Rhodes <brandon@ten22.rhodesmill.org> wrote:
...[color=blue][color=green]
> > 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])[/color]
>
> 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.[/color]

Yeah, so?
[color=blue]
> 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.[/color]

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'[color=blue]
> class mt:
> def __init__(self, seq): self.seq=list(seq)
> ' 'mt([ 1,2,3 ])'[/color]
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
Torsten Mohr
Guest
 
Posts: n/a
#5: Jul 18 '05

re: my own type in C, sequence protocol


Hi,
[color=blue][color=green]
>> Now that i have implemented the sequence protocol, what
>> can i do with it, can't i assign values like above somehow?[/color]
>
> 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++.[/color]

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.

Heiko Wundram
Guest
 
Posts: n/a
#6: Jul 18 '05

re: my own type in C, sequence protocol


Am Donnerstag, 9. September 2004 22:21 schrieb Torsten Mohr:[color=blue]
> 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?[/color]

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:
[color=blue][color=green][color=darkred]
>>> x = [1,2,3]
>>> id(x)[/color][/color][/color]
-1477186996[color=blue][color=green][color=darkred]
>>> x[:] = [4,5,6]
>>> x[/color][/color][/color]
[4, 5, 6][color=blue][color=green][color=darkred]
>>> id(x)[/color][/color][/color]
-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.
Grant Edwards
Guest
 
Posts: n/a
#7: Jul 18 '05

re: my own type in C, sequence protocol


On 2004-09-09, Torsten Mohr <tmohr@s.netic.de> wrote:
[color=blue]
> 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?[/color]

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.

--
Grant Edwards grante Yow! Is this where people
at are HOT and NICE and they
visi.com give you TOAST for FREE??
Grant Edwards
Guest
 
Posts: n/a
#8: Jul 18 '05

re: my own type in C, sequence protocol


On 2004-09-09, Grant Edwards <grante@visi.com> wrote:
[color=blue][color=green]
>> 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?[/color]
>
> 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.[/color]

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

--
Grant Edwards grante Yow! VICARIOUSLY
at experience some reason
visi.com to LIVE!!
Alex Martelli
Guest
 
Posts: n/a
#9: Jul 18 '05

re: my own type in C, sequence protocol


Grant Edwards <grante@visi.com> wrote:
...[color=blue]
> If you want to iterate over your object, you probably want to
> support the sequence protocol.[/color]

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
Closed Thread