I want to have a str with custom methods, but I have this problem:
class myStr(str):
def hello(self):
return 'hello '+self
s=myStr('world')
print s.hello() # prints 'hello world'
s=s.upper()
print s.hello() # expected to print 'hello WORLD', but s is no longer
myStr, it's a regular str!
What can I do? 5 1347
In <11**********************@k78g2000cwa.googlegroups .com>, ku********@gmail.com wrote:
I want to have a str with custom methods, but I have this problem:
class myStr(str):
def hello(self):
return 'hello '+self
s=myStr('world')
print s.hello() # prints 'hello world'
s=s.upper()
print s.hello() # expected to print 'hello WORLD', but s is no longer
myStr, it's a regular str!
What can I do?
Return a `myStr` instance instead of a regular `str`:
def hello(self):
return myStr('hello ' + self)
Ciao,
Marc 'BlackJack' Rintsch
Marc 'BlackJack' Rintsch wrote:
In <11**********************@k78g2000cwa.googlegroups .com>, ku********@gmail.com wrote:
>I want to have a str with custom methods, but I have this problem:
class myStr(str): def hello(self): return 'hello '+self
s=myStr('world') print s.hello() # prints 'hello world' s=s.upper() print s.hello() # expected to print 'hello WORLD', but s is no longer myStr, it's a regular str!
What can I do?
Return a `myStr` instance instead of a regular `str`:
def hello(self):
return myStr('hello ' + self)
yes, but the 'upper' method is the problem here.
So you'd have to override all string methods, like
class myStr(str):
...
def upper(self):
return myStr(str.upper(self))
And I'm not sure, if it then works in the intended way...
What you are probably looking for, is to extend the 'str' class itself, so
every str instance has your added functionality.
Don't know, if this is possible at all, but usually it's not a good idea to
mess with the bowels of Python unless you have some greater surgical
skills.
HTH
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
On Feb 5, 5:48 am, "kungfoo...@gmail.com" <kungfoo...@gmail.com>
wrote:
I want to have a str with custom methods, but I have this problem:
class myStr(str):
def hello(self):
return 'hello '+self
s=myStr('world')
print s.hello() # prints 'hello world'
s=s.upper()
print s.hello() # expected to print 'hello WORLD', but s is no longer
myStr, it's a regular str!
What can I do?
To prevent operations with your myStr class to return a simple str
instance, you will have to override pretty much all str method, as
well as magic methods, such as __add__, __radd__ etc.. Major pain in
perspective. You might want to reconsider you developing strategy.
On 5 Feb 2007 02:48:08 -0800, ku********@gmail.com <ku********@gmail.comwrote:
I want to have a str with custom methods, but I have this problem:
class myStr(str):
def hello(self):
return 'hello '+self
s=myStr('world')
print s.hello() # prints 'hello world'
s=s.upper()
print s.hello() # expected to print 'hello WORLD', but s is no longer
myStr, it's a regular str!
You could use the proxy pattern:
class GreeterString(object):
def __init__(self, str):
self.proxy = str
def hello(self):
return 'hello ' + self.proxy
def __getattr__(self, attr):
if attr in dir(self.proxy):
proxy_attr = getattr(self.proxy, attr)
if callable(proxy_attr):
def wrapper(*args, **kwargs):
return self.__class__(proxy_attr())
return wrapper
def __str__(self):
return self.proxy.__str__()
gs = GreeterString('world')
print gs.upper().hello()
Magic methods has to be overridden manually, I think.
--
mvh Björn
On 5 feb, 11:48, "kungfoo...@gmail.com" <kungfoo...@gmail.comwrote:
I want to have a str with custom methods, but I have this problem:
class myStr(str):
def hello(self):
return 'hello '+self
s=myStr('world')
print s.hello() # prints 'hello world'
s=s.upper()
print s.hello() # expected to print 'hello WORLD', but s is no longer
myStr, it's a regular str!
What can I do?
I'm new to this list(this is my first message) and to Python also (I'm
learning these days), so i'm afraid that this is not what you are
asking for but anyway
you can create an myStr object on the fly, something like:
s=myStr(s.upper())
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fuzzyman |
last post by:
Hello,
To create a classic (old style) class, I write :
class foo:
pass
To do the equivalent as a new style class, I write :
class foo(object):
|
by: BCC |
last post by:
Hi,
A colleague has some code like this:
class CMyObject {
// Bunch of Member functions
}
class CMyObjectList: public std::vector<CMyObject>
{
|
by: JustSomeGuy |
last post by:
this doesn't want to compile....
class image : public std::list<element>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element...
|
by: shaun roe |
last post by:
I want something which is very like a bitset<64> but with a couple of
extra functions: set/get the two 32 bit words, and conversion to
unsigned long long.
I can do this easily by inheriting from...
|
by: Noah Coad [MVP .NET/C#] |
last post by:
How do you make a member of a class mandatory to override with a _new_
definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certain...
|
by: Shayne H |
last post by:
I wanted to create a type-safe collection by inheriting from CollectionBase.
Public Class MyCollection : Inherits CollectionBase
I found that IList.Add did not allow the setting of a "key" to...
|
by: Jim Heavey |
last post by:
I amd playing around with inheritance a little in VB.Net
If I create a new class which inherits from ListViewItem and I am only
wanting to override the ToString Method. In this situation, If I...
|
by: Alex Satrapa |
last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg:
CREATE TABLE foo (
id SERIAL PRIMARY KEY
);
CREATE TABLE...
|
by: Fabiano Sidler |
last post by:
Hi folks!
As stated in subject, how do I decide wether to inherit <type 'type'> or
<type 'object'>? Whenever I want to intantiate my derived type, I taked
<type 'type'> here, but inheriting from...
|
by: Adrian Hawryluk |
last post by:
Hi all,
What is everyone's opinion of const inheriting? Should the object that
a pointer is pointing at inherit the constness of the pointer? Such as
in the case of a class having a pointer...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |