473,563 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1460
In article <ch**********@s chleim.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.washingt on.edu
Jul 18 '05 #2
Donn Cave <do**@u.washing ton.edu> writes:
In article <ch**********@s chleim.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*****@rhodesm ill.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(s eq)
' '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(s eq)
' '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.co m> 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.co m> 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
2990
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 - A Proposal As we all know, one of the best things about python and other scripting languages is dynamic typing (yes I know it has advantages...
3
1991
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; len = 2;
2
1747
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 instance of a given PyTypeObject MyType ? I have tried to provide (PyObject *)&MyType as 'class' argument to: PyInstance_New(PyObject *class,...
2
3287
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 xsi:type="DerivedClass" attribute and the Instance Namespace. When I attempt to validate the xml upon deserialization the XmlValidatingReader chokes...
2
7719
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 cannot extract all of the type information from the wsdl using any standard tools. I'm using HttpGet style webservices if that makes any difference,...
2
1875
by: Martin v. Löwis | 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: $Revision: 42333 $
669
25640
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
0
1146
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 name="Magazine" abstract="true"> <xs:sequence> <xs:element name="Title" type="xs:string"/> <xs:element name="Editor" type="xs:string"/>
7
2433
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. I then wanted to let callers slice from an iterator's position, but that isn't supported without creating a custom iterator class. Are there...
0
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.