My fight with classes :)

TheSaint fc14301589 at icqmail.com
Wed Jun 11 10:16:56 EDT 2008


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



More information about the Python-list mailing list