473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2.2.2 Annoyance

Caveat:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34)
Type "help", "copyright" , "credits" or "l
l = [1,2,3]
l[1] 2 l.__getitem__(1 ) 2 l.__getitem__(s lice(1)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: an integer is required


According to the manuals of Python 2.2.2, passing a slice object to
__getitem__ is all right and should work...
Cost a few (grey) hairs of mine, while debugging a largish program, this is
not so.
Would someone care to pass on this info to whom this may concern?

For Python 2.3 passing a slice object to __getitem__ does work though.

Best,
Miklós

Jul 18 '05 #1
9 1482

"Jegenye 2001 Bt" <no****@nowhere .hu> wrote in message
news:bp******** **@namru.matavn et.hu...
According to the manuals of Python 2.2.2, passing a slice object to .... Would someone care to pass on this info to whom this may concern?
2.2.2 was superceded by 2.2.3. This info is only of concern to people
who have and will not upgrade to 2.3.
For Python 2.3 passing a slice object to __getitem__ does work

though.

2.3 fixed more bugs beyond 2.2.3, added a couple of builtins, and
several modules. This change in behavior between 2.2.2 and 2.3 was
almost certainly intentional, due someone already noticing what you
did.

tjr
Jul 18 '05 #2

Terry Reedy <tj*****@udel.e du> wrote in message
news:EI******** ************@co mcast.com...

"Jegenye 2001 Bt" <no****@nowhere .hu> wrote in message
news:bp******** **@namru.matavn et.hu...
According to the manuals of Python 2.2.2, passing a slice object to ...
Would someone care to pass on this info to whom this may concern?


2.2.2 was superceded by 2.2.3. This info is only of concern to people
who have and will not upgrade to 2.3.


I hadn't known about 2.2.3* and 2.3 was/is (?) out at that site for the time
being.

* thanx, I will try it .. even though I have already patched the code by
'try/except's

Miklós
tjr

Jul 18 '05 #3
"Jegenye 2001 Bt" <no****@nowhere .hu> writes:
Caveat:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34)
Type "help", "copyright" , "credits" or "l
l = [1,2,3]
l[1] 2 l.__getitem__(1 ) 2 l.__getitem__(s lice(1)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: an integer is required

According to the manuals of Python 2.2.2, passing a slice object to
__getitem__ is all right and should work...


Really? Where does it say that?
For Python 2.3 passing a slice object to __getitem__ does work though.


Yes. Do you have a good reason for sticking to 2.2?

Cheers,
mwh

--
Python enjoys making tradeoffs that drive *someone* crazy <wink>.
-- Tim Peters, comp.lang.pytho n
Jul 18 '05 #4

Michael Hudson <mw*@python.net > wrote in message
news:m3******** ****@pc150.math s.bris.ac.uk...
"Jegenye 2001 Bt" <no****@nowhere .hu> writes:
According to the manuals of Python 2.2.2, passing a slice object to
__getitem__ is all right and should work...
Really? Where does it say that?


Python Reference Manual,
3.3.4 Emulating container types
(Release 2.2.2, documentation updated on October 14, 2002. )
"""
__getitem__(sel f, key)
Called to implement evaluation of self[key]. For sequence types, the
accepted keys should be integers and slice objects. Note that the special
interpretation of
"""
For Python 2.3 passing a slice object to __getitem__ does work though.
Yes. Do you have a good reason for sticking to 2.2?

Yes, see my previous post on this. (That site wants to stay with 2.2 for the
time being.)

Actually I patched the code to circumvent this and I haven't tried 2.2.3 as
for this yet. (I didn't know about 2.2.3 until Terry's response..)

All this is no big deal ... unless you pull too many hairs of your receding
head because of a bug like this. :-))

Cheers,
Miklós
Cheers,
mwh

--
Python enjoys making tradeoffs that drive *someone* crazy <wink>.
-- Tim Peters, comp.lang.pytho n

Jul 18 '05 #5
"Miklós" <no****@nowhere .hu> writes:
Michael Hudson <mw*@python.net > wrote in message
news:m3******** ****@pc150.math s.bris.ac.uk...
"Jegenye 2001 Bt" <no****@nowhere .hu> writes:
According to the manuals of Python 2.2.2, passing a slice object to
__getitem__ is all right and should work...
Really? Where does it say that?


Python Reference Manual,
3.3.4 Emulating container types
(Release 2.2.2, documentation updated on October 14, 2002. )
"""
__getitem__(sel f, key)
Called to implement evaluation of self[key]. For sequence types, the
accepted keys should be integers and slice objects. Note that the special
interpretation of
"""


I think this is talking about implementing __getitem__, not calling it
directly. Historically these things have been quite different, though
they are becoming less so.
For Python 2.3 passing a slice object to __getitem__ does work though.


Yes. Do you have a good reason for sticking to 2.2?

Yes, see my previous post on this.


I haven't had the time to read all posts to this ng for some time...
(That site wants to stay with 2.2 for the time being.)
OK.
Actually I patched the code to circumvent this and I haven't tried 2.2.3 as
for this yet. (I didn't know about 2.2.3 until Terry's response..)


2.2.3 will not make any difference on this point.

Cheers,
mwh

--
ARTHUR: Don't ask me how it works or I'll start to whimper.
-- The Hitch-Hikers Guide to the Galaxy, Episode 11
Jul 18 '05 #6
Michael Hudson <mw*@python.net > wrote in message
news:m3******** ****@pc150.math s.bris.ac.uk...
"Miklós" <no****@nowhere .hu> writes:
"""
__getitem__(sel f, key)
Called to implement evaluation of self[key]. For sequence types, the
accepted keys should be integers and slice objects. Note that the special interpretation of
"""
I think this is talking about implementing __getitem__, not calling it
directly. Historically these things have been quite different, though
they are becoming less so.

Well, I've never been really into the guts of Python ... but could you shed
some light for me on that why these two things are different?
What I experienced was this "feature" broke my code (actually it was in a
3rd party code library) and it took a while for me to recognize the
documentation was wrong
(or whatever, depending on your explanation..) and slice objects shouldn't
be passed to __getitem__ that way in Python 2.2
(and that this works for Python 2.3)

2.2.3 will not make any difference on this point.
Well, then my patches will stay and slow down things a bit.
Cheers,
Miklós


Cheers,
mwh

--
ARTHUR: Don't ask me how it works or I'll start to whimper.
-- The Hitch-Hikers Guide to the Galaxy, Episode 11

Jul 18 '05 #7
"Miklós" <no****@nowhere .hu> writes:
Michael Hudson <mw*@python.net > wrote in message
news:m3******** ****@pc150.math s.bris.ac.uk...
"Miklós" <no****@nowhere .hu> writes:
"""
__getitem__(sel f, key)
Called to implement evaluation of self[key]. For sequence types, the
accepted keys should be integers and slice objects. Note that the special interpretation of
"""


I think this is talking about implementing __getitem__, not calling it
directly. Historically these things have been quite different, though
they are becoming less so.

Well, I've never been really into the guts of Python ... but could you shed
some light for me on that why these two things are different?


Hmm, that's potentially a large topic. I'll have a go.

At the C implementation level, Python distinguishes sequences (lists,
strings, tuples, arrays, etc) and mappings (dicts, basically).
However, Python-the-language uses the same notation -- brackets, [] --
to access the elements of both sequences and mappings, and there's
only one corresponding special method -- __getitem__.

If you're implementing a sequence in C, you're expected to fill out
the tp_as_sequence structure of the type object, which contains these
fields:

typedef struct {
inquiry sq_length;
binaryfunc sq_concat;
intargfunc sq_repeat;
intargfunc sq_item;
intintargfunc sq_slice;
intobjargproc sq_ass_item;
intintobjargpro c sq_ass_slice;
objobjproc sq_contains;
/* Added in release 2.0 */
binaryfunc sq_inplace_conc at;
intargfunc sq_inplace_repe at;
} PySequenceMetho ds;

Notice that "sq_item" -- the function that retrieves an element of the
sequence -- is an "intargfunc ", which is:

typedef PyObject *(*intargfunc)( PyObject *, int);

and "sq_slice" is an "intintargfunc" , i.e:

typedef PyObject *(*intintargfun c)(PyObject *, int, int);

So, there's no way to actually implement a sequence that directly
handles a slice object! You have to make a mapping instead (the
corresponding function in PyMappingMethod s is mp_subscript, a
binaryfunc:

typedef PyObject * (*binaryfunc)(P yObject *, PyObject *);

). This is the first reason you can't pass slice objects to
list.__getitem_ _ in 2.2 -- list just didn't implement mp_subscript.

The other reason is: just what is "list.__getitem __"? If list was
implemented in Python, it would be obvious, it would be an unbound
method. But list *isn't* implementing in Python, list is implemented
in C, so list.__getitem_ _ is a thing called a method-wrapper. When
you access it, magic happens to find the appropriate C level method
and wrap it up so you can call it from Python. But for __getitem__
specifically, there are two choices, sq_item and mp_subscript! 2.2
prefers sq_item, so even after the list type grew a mp_subscript
function [1].__getitem__(sl ice(0,1)) failed, because the
method-wrapper for sq_item didn't know what to do with the slice
object. 2.3 prefers mp_subscript over sq_item.

Hope that helped!

Cheers,
mwh

--
The "of course, while I have no problem with this at all, it's
surely too much for a lesser being" flavor of argument always
rings hollow to me. -- Tim Peters, 29 Apr 1998
Jul 18 '05 #8

Michael Hudson <mw*@python.net > wrote in message
news:m3******** ****@pc150.math s.bris.ac.uk...
"Miklós" <no****@nowhere .hu> writes:

[long explanation deleted]

Huh, that was really tough reading for an un-C-er..
Still I think I savvy the thing: there was a semantic change at the
underlying C implementation level.

I understand the rationale for deprecating '__getslice__' from v2.0
onwards was to make sequences and mappings to look more
alike. But then this change for __getitem__ should have been absolutely
introduced at that time or the semantics of CPython between 2.0 and 2.2
doesn't make sense in this respect... Am I right in that?
Hope that helped!
Yes, I think so. :) Many thanks.
Best,
Miklós
--
Jegenye 2001 Bt.
'amVnZW55ZTIwMD FAcGFya2hvc3Rpb mcuY29t\n'.deco de('base64')
Egyedi szoftverkészíté s, tanácsadás
Custom software development, consulting
http://jegenye2001.parkhosting.com

Cheers,
mwh


Jul 18 '05 #9
"Miklós" <no****@nowhere .hu> writes:
I understand the rationale for deprecating '__getslice__' from v2.0
onwards was to make sequences and mappings to look more alike. But
then this change for __getitem__ should have been absolutely
introduced at that time or the semantics of CPython between 2.0 and
2.2 doesn't make sense in this respect... Am I right in that?


Well, sort of. Back in the 2.0 days you couldn't even write:
[].__getitem__
so
[].__getitem__(sl ice(0,1))


was right out! It's all a bit messy, and it's sorted out in 2.3.

Cheers,
mwh

--
Monte Carlo sampling is no way to understand code.
-- Gordon McMillan, comp.lang.pytho n
Jul 18 '05 #10

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

Similar topics

8
1314
by: caroundw5h | last post by:
PEP 292 adds a Template class to the string module that uses "$" to indicate a substitution. Template is a subclass of the built-in Unicode type, so the result is always a Unicode string: >>> import string >>> t = string.Template('$page: $title') >>> t.substitute({'page':2, 'title': 'The Best of Times'}) u'2: The Best of Times' If a key is missing from the dictionary, the substitute method will
0
1354
by: Bloody Viking | last post by:
I've got an XSLT stylesheet which I've written and maintained for over 2 years now. I've always simply processed it with java org.apache.xalan.xslt.Process, but now I'm trying to speed things up, by compiling the stylesheet once, and processing hundreds of XML docs with it. The output is HTML. This is Java 1.4.1 on a Sparc9. The relevant code: in main():
16
1724
by: sloughin | last post by:
I realize that C# is a strongly typed language, but as a C++ programmer converting to C# I find the requirement that I typecast my integer enum values to be particularly annoying. Does anyone else in the C# community think that the compiler should relax this a bit // example, define an enum that derives from an int type public enum Fruit : in Apple = 1 Banana = 2 Cherry = 3 Date = 4
5
2551
by: Wiseguy | last post by:
consider the following: ostream_iterator<int> i(cout,""); cout << setw(3); copy(int_list.begin(),int_list.end(),i); the problem is that the field width restriction is only active for the first element of int_list. after the first element is copied to cout the stream setw(3) no longer has any affect.
0
1405
by: PL | last post by:
It seems there is a bug or at least an annoynance with the new eventvalidation. I can't really tell you a certain way to reproduce this but I have a large datagrid that uses custom paging, I have navigation controls both at the top and the bottom. Everything works fine if you click the navigation links and allow the page to fully load before clicking the next time, but sometimes people click the navigation links at the top before the...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2130
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.