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(valu e, str): return
if value.lower().s tartswith('a'):
self.valueA += value
if value.lower().s tartswith('d'):
self.valueD += value
return self.valueA ,self.valueD
__call__= __add__
__iadd__= __add__
my test on the shell:
<utilities.StrJ oin instance at 0x9dc7ccc>
('aks', '')
('aks', 'daks')
('aks', 'daks')
('aks', 'daksdhks')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
('aks', 'daksdhks')
('aksaboi', 'daksdhks')
('aksaboi', 'daksdhksduboi' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple
('aksaboi', 'daksdhksduboi' )
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
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(valu e, str): return
if value.lower().s tartswith('a'):
self.valueA += value
if value.lower().s tartswith('d'):
self.valueD += value
return self.valueA ,self.valueD
__call__= __add__
__iadd__= __add__
my test on the shell:
>>from utilities import StrJoin as zx
>>k= zx()
>>k
>>k= zx()
>>k
>>k +'aks'
>>k +'daks'
>>k +'hdaks'
>>k +'dhks'
>>j('boi')
File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
>>k('boi')
>>k('aboi')
>>k('duboi')
>>k += 'liu'
>>k += 'aliu'
>>k += 'aliu'
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple
>>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
Comment