473,394 Members | 1,697 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,394 software developers and data experts.

string methods of a str subclass

I am probably misunderstanding some basic issue here but this
behaviour is not what I would expect:

Python 2.4 (#1, Mar 22 2005, 21:42:42)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>class mystr( str ):
.... pass
....
>>x = mystr( 'x' )
isinstance( x, mystr )
True
>>isinstance( x.strip( ), mystr )
False
>>>

Why is the strip( ) method returning something that is not a mystr
instance? I would expect all methods operating on a string instance
and returning another string instance to correctly operate on a mystr
instance and return a mystr instance. How would I achieve something
like this without manually copying all string returning methods from
str and stuffing the result to mystr( ) before returning?
Apr 16 '07 #1
4 2135
On Apr 16, 3:28 am, "Daniel Nogradi" <nogr...@gmail.comwrote:
I am probably misunderstanding some basic issue here but this
behaviour is not what I would expect:

Python 2.4 (#1, Mar 22 2005, 21:42:42)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>class mystr( str ):

... pass
...
>x = mystr( 'x' )
isinstance( x, mystr )
True
>isinstance( x.strip( ), mystr )
False

Why is the strip( ) method returning something that is not a mystr
instance? I would expect all methods operating on a string instance
and returning another string instance to correctly operate on a mystr
instance and return a mystr instance. How would I achieve something
like this without manually copying all string returning methods from
str and stuffing the result to mystr( ) before returning?
class A(object):
def __init__(self, s):
self.s = s
def strip(self):
return 2

class mystr(A):
pass

x = mystr("x")
print isinstance(x, mystr)
print isinstance(x.strip(), mystr)
Apr 16 '07 #2
"Daniel Nogradi" <no*****@gmail.comwrote:
Why is the strip( ) method returning something that is not a mystr
instance? I would expect all methods operating on a string instance
and returning another string instance to correctly operate on a mystr
instance and return a mystr instance.
Why would you expect that?
Would you expect the __str__ and__repr__ methods also to return a mystr
instance? If not those, then which other ones might also be excluded?
Is x.encode('zip') still a mystr instance or an encoded byte-string?
How would I achieve something
like this without manually copying all string returning methods from
str and stuffing the result to mystr( ) before returning?
You don't without wrapping all the affected methods. It doesn't need to
involve manual copying though: you have a programming language available so
just write a list of method names and then some code to wrap them
automatically.
Apr 16 '07 #3
On Apr 16, 3:28 am, "Daniel Nogradi" <nogr...@gmail.comwrote:
I would expect all methods operating on a string instance
and returning another string instance
Ok, then this:

class A(object):
def __init__(self, s):
self.s = s
def strip(self):
return self.s

class mystr(A):
pass

x = mystr("x")
print isinstance(x, mystr)
print isinstance(x.strip(), mystr)
"x" is a string, and that is what gets passed to the base class's
__init__ method, and that is what strip() operates on.
Apr 16 '07 #4
Why is the strip( ) method returning something that is not a mystr
instance? I would expect all methods operating on a string instance
and returning another string instance to correctly operate on a mystr
instance and return a mystr instance.

Why would you expect that?
Would you expect the __str__ and__repr__ methods also to return a mystr
instance? If not those, then which other ones might also be excluded?
Is x.encode('zip') still a mystr instance or an encoded byte-string?
Okay, good point, thanks.
How would I achieve something
like this without manually copying all string returning methods from
str and stuffing the result to mystr( ) before returning?

You don't without wrapping all the affected methods. It doesn't need to
involve manual copying though: you have a programming language available so
just write a list of method names and then some code to wrap them
automatically.
Yes, this is in fact what I am doing, using __getattr__ and such. Thanks again.
Apr 16 '07 #5

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
5
by: zero | last post by:
I'm having trouble with overriding methods in subclasses. I'll explain the problem using some code: class BaseClass { protected: void method2(); public: void method1();
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
3
by: Hallvard B Furuseth | last post by:
I'm wondering how to design this: An API to let a request/response LDAP server be configured so a user-defined Python module can handle and/or modify some or all incoming operations, and later...
17
by: Donn Ingle | last post by:
Hi, Here's a framework for the questions: --- In a module, part of an API --- class Basis ( object ): def foo ( self, arg ): pass --- In user's own code --- class Child ( Basis ):
4
by: RinKaMeAri | last post by:
Hi! Could you imagine any way to block access to the base class public methods? Here is an example: class B: def public_method(): pass class A(B): def public_a_method():
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: 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: 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
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
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...

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.