473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

my own type in C, sequence protocol

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.

Jul 18 '05 #1
8 1450
In article <ch**********@schleim.qwe.de>,
Torsten Mohr <tm***@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, do**@u.washington.edu
Jul 18 '05 #2
Donn Cave <do**@u.washington.edu> writes:
In article <ch**********@schleim.qwe.de>, 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.

--
Brandon Craig Rhodes br*****@rhodesmill.org http://rhodesmill.org/brandon
Jul 18 '05 #3
Brandon Craig Rhodes <br*****@ten22.rhodesmill.org> wrote:
...
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
Jul 18 '05 #4
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.

Jul 18 '05 #5
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.
Jul 18 '05 #6
On 2004-09-09, Torsten Mohr <tm***@s.netic.de> wrote:
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.

--
Grant Edwards grante Yow! Is this where people
at are HOT and NICE and they
visi.com give you TOAST for FREE??
Jul 18 '05 #7
On 2004-09-09, Grant Edwards <gr****@visi.com> wrote:
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.

--
Grant Edwards grante Yow! VICARIOUSLY
at experience some reason
visi.com to LIVE!!
Jul 18 '05 #8
Grant Edwards <gr****@visi.com> wrote:
...
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
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
3
by: Matjaz | last post by:
Dear all, I have run into a problem using python lists and sequence protocol. The first code snippet uses explicit list operations and works fine. PyObject *argseq, *ov; int i, v, len; ...
2
by: BranoZ | last post by:
I'm writing my own (list-like) type in C. It is implementing a Sequence Protocol. In 'sq_slice' method I would like to return a new instance of my class/type. How do I create (and initialize) an...
2
by: Jason Cartwright | last post by:
I have an abstract base class and two derived classes that I want to serialize and deserialize with schema validation. When I serialize instances of the derived classes the XmlSerializer adds the...
2
by: Danny Gagne | last post by:
I'm currently working an .net application (I can use 1.1 or 2.0 if needed) that needs to read a wsdl file and generate another piece of code that can use it. I'm encountering a problem where I...
2
by: Martin v. Lwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
0
by: jam_planta | last post by:
Hi guys, Hope you can help me.I'm new in using C# and Xml Schema. I have a problem in getting the child element of the base type.. Here is the sample of my xsd file: <xs:complexType...
7
by: schickb | last post by:
I think it would be useful if iterators on sequences had the __index__ method so that they could be used to slice sequences. I was writing a class and wanted to return a list iterator to callers. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.