473,397 Members | 2,033 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,397 software developers and data experts.

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__(slice(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 1466

"Jegenye 2001 Bt" <no****@nowhere.hu> wrote in message
news:bp**********@namru.matavnet.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.edu> wrote in message
news:EI********************@comcast.com...

"Jegenye 2001 Bt" <no****@nowhere.hu> wrote in message
news:bp**********@namru.matavnet.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__(slice(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.python
Jul 18 '05 #4

Michael Hudson <mw*@python.net> wrote in message
news:m3************@pc150.maths.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__(self, 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.python

Jul 18 '05 #5
"Miklós" <no****@nowhere.hu> writes:
Michael Hudson <mw*@python.net> wrote in message
news:m3************@pc150.maths.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__(self, 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.maths.bris.ac.uk...
"Miklós" <no****@nowhere.hu> writes:
"""
__getitem__(self, 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.maths.bris.ac.uk...
"Miklós" <no****@nowhere.hu> writes:
"""
__getitem__(self, 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;
intintobjargproc sq_ass_slice;
objobjproc sq_contains;
/* Added in release 2.0 */
binaryfunc sq_inplace_concat;
intargfunc sq_inplace_repeat;
} PySequenceMethods;

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 *(*intintargfunc)(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 PyMappingMethods is mp_subscript, a
binaryfunc:

typedef PyObject * (*binaryfunc)(PyObject *, 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__(slice(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.maths.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.
'amVnZW55ZTIwMDFAcGFya2hvc3RpbmcuY29t\n'.decode('b ase64')
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__(slice(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.python
Jul 18 '05 #10

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

Similar topics

8
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: >>>...
0
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,...
16
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...
5
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...
0
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...
0
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,...

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.