Re: My fight with classes :)



On Wed, 11 Jun 2008 22:16:56 +0800, TheSaint <fc14301589@xxxxxxxxxxx> wrote:
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:

[snip]
k +'aks'
('aks', '')
k +'daks'
('aks', 'daks')
[snip]
k += 'liu'
k += 'aliu'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple

You have designed your class in a confusing way, and then you
were confused by it.

It was confusing to design your class in such a way that "k+'aks'"
modifies k, because "k+'aks'" appears to be just an expression.
Changing k as a side-effect of evaluating that expression is
unnecessarily confusing. See if you can figure out how to design
your class so that you modify k either by writing "k.append( 'aks' )"
or "k += 'aks'".

Speaking of which, you discovered that "k += 'liu';
k += 'aliu'" fails. It fails because "k += 'liu'" replaces k
with a tuple (because you return a tuple from your __add__
function), so k is no longer an instance of your class.


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.

Classes are wonderfully useful, and it would be sad for you to
use Python while shunning classes. I strongly recommend looking
at some good examples of simple class programming; I apologize
for having no specific recommendations.

--
To email me, substitute nowhere->spamcop, invalid->net.
.



Relevant Pages

  • Re: Stuck on creating an easy view, please help
    ... you have to reconsider your design. ... > concatenated to form a string for the team, ... > Request_id INT NOT NULL, ... > resultset & do the concatenation leveraging the client programming ...
    (microsoft.public.sqlserver.programming)
  • Re: Stuck on creating an easy view, please help
    ... you have to reconsider your design. ... concatenated to form a string for the team, ... INSERT Requests SELECT 240, 3; ... resultset & do the concatenation leveraging the client programming ...
    (microsoft.public.sqlserver.programming)
  • Re: 7 December 1941
    ... > You seem to be confusing date of design with date of issue. ... determination that while an 8" Howitzer was a Good Thing, ... pre Lend-Lease) to the British and the Finns. ...
    (rec.aviation.military)
  • Re: Whats so special about operators, built-in classes and modules?
    ... will prevent people from modifiying string. ... Aside from the issue of C strings, I'd say the way const is used in functions is b0rken in C. Take int foo; Since bar is passed by value, any modifications of it in the body of the function are only local to the function. ... And the syntax for that is really confusing -- generally the syntax for pointers is really confusing in C as a general rule. ... As for multiple inheritance vs. single inheritance, I think the problem isn't a technical one, it's a human one. ...
    (comp.lang.ruby)
  • Re: Function shortcut help
    ... Helmut, and Tony: ... Sorry for the confusing post, ... Public Function MyResult(x As String, ...
    (microsoft.public.word.vba.general)