My fight with classes :)



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:

from utilities import StrJoin as zx
k= zx()
k
<utilities.StrJoin instance at 0x9dc7ccc>
k +'aks'
('aks', '')
k +'daks'
('aks', 'daks')
k +'hdaks'
('aks', 'daks')
k +'dhks'
('aks', 'daksdhks')
j('boi')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
k('boi')
('aks', 'daksdhks')
k('aboi')
('aksaboi', 'daksdhks')
k('duboi')
('aksaboi', 'daksdhksduboi')
k += 'liu'
k += 'aliu'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple
k
('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
.



Relevant Pages

  • Re: Does VBscript support " & _" for long strings?
    ... concatenation in vb/vbscript. ... iteratively appending values to a string. ... Dim a, b, bb, i ... str = str & Space ...
    (microsoft.public.scripting.vbscript)
  • Re: syntax...
    ... B&D on the part of the language designer. ... probably handle concatenation of string literals by itself, ... bitwise XOR, or if not that, then exponentiation.) ...
    (comp.lang.misc)
  • Bugs in the Module::Dependency
    ... Manifying blib/man1/pmd_indexer.plx.1 ... Use of uninitialized value in concatenation or string at ... # Failed test in t/04grapher.t at line 81. ...
    (perl.dbi.users)
  • RE: Simple regex problem has me baffled
    ... I am writing a script to analyse a log file. ... Each pair of Request and Response calls have a unique ... Use of uninitialized value in concatenation or string at ...
    (perl.beginners)
  • Re: Literal concatenation, strings vs. numbers (was: Numeric literals in other than base 10 - wa
    ... Well my take on it is that this would not be the same as string ... concatenation, the series of digits would be parsed as a single token ... digit separation can superficially resemble string concatenation if ... I would favor _ as a digit separator in Python ...
    (comp.lang.python)