My fight with classes :)
- From: TheSaint <fc14301589@xxxxxxxxxxx>
- Date: Wed, 11 Jun 2008 22:16:56 +0800
Hi,
I'm very new with classes. I still reading something around ;)
I got started to try a concatenation of 2 type of string, which have a
particular property to start with A or D.
My class here:
""" Small class to join some strings according to the leading first
letter"""
def __init__(self):
self.valueA= ''
self.valueD= ''
def __add__(self, value):
if not isinstance(value, str): return
if value.lower().startswith('a'):
self.valueA += value
if value.lower().startswith('d'):
self.valueD += value
return self.valueA ,self.valueD
__call__= __add__
__iadd__= __add__
my test on the shell:
<utilities.StrJoin instance at 0x9dc7ccc>from utilities import StrJoin as zx
k= zx()
k
('aks', '')k +'aks'
('aks', 'daks')k +'daks'
('aks', 'daks')k +'hdaks'
('aks', 'daksdhks')k +'dhks'
Traceback (most recent call last):j('boi')
File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
('aks', 'daksdhks')k('boi')
('aksaboi', 'daksdhks')k('aboi')
('aksaboi', 'daksdhksduboi')k('duboi')
Traceback (most recent call last):k += 'liu'
k += 'aliu'
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple
('aksaboi', 'daksdhksduboi')k
Do I miss something?
I'd rather like to avoid class, but a function won't allow me to store so
easily data between several call.
Mostly I'd expect to pass to the built instance in a more elaborated
function. Then I mean call will be the primer goal.
--
Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html
.
- Follow-Ups:
- Re: My fight with classes :)
- From: Terry Reedy
- Re: My fight with classes :)
- From: Peter Pearson
- Re: My fight with classes :)
- Prev by Date: Re: problems with opening files due to file's path
- Next by Date: Re: can't assign to literal
- Previous by thread: Numpy array to gzip file
- Next by thread: Re: My fight with classes :)
- Index(es):
Relevant Pages
|