From: Alexander on
On 18.04.2010 13:23, Steven D'Aprano wrote:
> On Sat, 17 Apr 2010 19:55:44 +0400, Alexander wrote:
>> Ok, I'll try to explain on the following example. Let's consider class
>> MyClass that holds one string and concatenate it with other not defined
>> in this class:
> [...]
>> and with working application:
>> a = MyClass()
>> a.set('key1')
>> b1 ='value1')
>> b2 ='value2')
>> print b1, "," ,b2 # give 'key1 value1 , key1 value2'
>> a.set('key2')
>> print b1, ",", b2 # give 'key2 value1 , key2 value2'
> Looking at your design, I can't see any reason for SubClass to be a
> subclass of MyClass. It doesn't inherit any behaviour, and the
> constructor takes completely different arguments. Why make it a Subclass?
It was a very simple example that express the crux of my question. There
isn't any sense discuss design on it. In real implementation SubClass
has almost the same functionality and methods as MyClass, the difference
only in a state variable (one instance has one state and second the
other, see example below).
> MyClass is dangerous: creating an instance doesn't fully initialise the
> instance, it leaves it in a half-initialised state that can cause
> exceptions from simple operations like:
> instance = MyClass()
> print instance
> This is very bad design.
This actualy depends on practical implementation of class. There could
be a checkup in __str__() member for presence of variable in instance
and result of each evaluation of instance.__str__() could be a valid:

class MyClass(object):
def __init__(s):
s.__default = 'value3'
def set(s, key):
s.__key = key
def __str__(s):
if '__other' in s.__dict__:
return s.__key + ' ' + s.__other
return s.__key + ' ' + s.__default
def new(s, value):
return SubClass(s, value)

Assume MyClass implementation is already given and SubClass have to be
implemented in some way.