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

Problem redefining __getitem__ for str subclass

I do not think I am understanding how to redefine the getitem function
for string. Why does the following not work:

class NStr(str):
def __getitem__(self,idx):
print "NStr:getitem",idx,type(idx)
return str.__getitem__(self,idx)

s=NStr("abcde")

print s[1]
print s[1:4:2]
print s[1:2]

if I run this program (python 2.5; Mac OSX) i get:

$ python strProb.py
NStr:getitem 1 <type 'int'>
b
NStr:getitem slice(1, 4, 2) <type 'slice'>
bd
b

ie the last statement (s[1:2]) with a simple slice does not call the
new __getitem__???? What am I missing.

Thanks

tom

Apr 22 '07 #1
3 2060
Hi!

Same result, with Python 2.5.1 on win-XP;
I have compassion with you.

--
@-salutations

Michel Claveau
Apr 22 '07 #2
<ts*****@gmail.comwrote:
I do not think I am understanding how to redefine the getitem function
for string. Why does the following not work:

class NStr(str):
def __getitem__(self,idx):
print "NStr:getitem",idx,type(idx)
return str.__getitem__(self,idx)

s=NStr("abcde")

print s[1]
print s[1:4:2]
print s[1:2]

if I run this program (python 2.5; Mac OSX) i get:

$ python strProb.py
NStr:getitem 1 <type 'int'>
b
NStr:getitem slice(1, 4, 2) <type 'slice'>
bd
b

ie the last statement (s[1:2]) with a simple slice does not call the
new __getitem__???? What am I missing.
For backwards compatibility, slicing without a step on some ancient
built-in types (including strings, but also lists) goes to __getslice__
(an otherwise obsolete method) instead of __getitem__. Simply, override
__getslice__ by properly delegating from it to __getitem__.
Alex
Apr 22 '07 #3
On 21 Apr 2007 20:36:13 -0700, tsm8015 wrote
I do not think I am understanding how to redefine the getitem
function for string. Why does the following not work:

class NStr(str):
def __getitem__(self,idx):
print "NStr:getitem",idx,type(idx)
return str.__getitem__(self,idx)

s=NStr("abcde")

print s[1]
print s[1:4:2]
print s[1:2]

if I run this program (python 2.5; Mac OSX) i get:

$ python strProb.py
NStr:getitem 1 <type 'int'>
b
NStr:getitem slice(1, 4, 2) <type 'slice'>
bd
b

ie the last statement (s[1:2]) with a simple slice does not call the
new __getitem__???? What am I missing.
You're overriding __getitem__ just fine, but if your object has a __getslice__
method, __getslice__ will be called instead of __getitem__ to fetch simple
slices. Since you are deriving from str which does implement __getslice__,
you'll need to override __getslice__, too.

-Carsten

Apr 22 '07 #4

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

Similar topics

4
by: KanZen | last post by:
I'm trying to understand the difference between __setitem__ and an ordinary method. For example: >>> class A(object): def __getitem__(self, *args): print len(args) def normalMethod(self,...
0
by: Mike Cannon | last post by:
I'm having a problem with the newer version of win32all and Python 2.3 with code that works fine on Python-2.2 (details below). Both machines are Windows XP. The code that breaks is derived...
5
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
7
by: Christian Neumann | last post by:
Hello, i have a problem with the built-in function xrange(). Could you by any chance be able to help? I use Python 2.3.4 (final) and i think there is a bug in the built-in
0
by: It's me | last post by:
I've built a Python application using PythonCard 1.9 and Python 2.3 running under Windows XP. Everything works except that when I use the keyboard instead of the mouse to do certain operations in...
1
by: simon | last post by:
What i'm trying to do is tie special methods of a "proxy" instance to another instance: def test1(): class Container: def __init__( self, data ): self.data = data self.__getitem__ =...
21
by: ron | last post by:
Why doesn't this work? >>> def foo(lst): .... class baz(object): .... def __getitem__(cls, idx): return cls.lst .... __getitem__=classmethod(__getitem__) .... baz.lst = lst .... ...
1
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
7
by: David Isaac | last post by:
I have a subclass of dict where __getitem__ returns None rather than raising KeyError for missing keys. (The why of that is not important for this question.) I was delighted to find that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...

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.