I've some nontrivial class implementation MyClass and its instance my:
my = MyClass(args)
MyClass uses in internals some variable which is not defined in MyClass
itself. I want to extend instance of MyClass at runtime defining this
variable and making new instance. It is like a class inheritance in a
static way
class MyNewClass(MyClass):
def __init__(s, a):
s._variable = a
but this doesn't give me ability to make inheritance at runtime of the
single parent intance. Finaly this should look like this
my = MyClass(args)
and e.t.c. Is it possible to release this interface in python?
> Hi, list.
>
> I've some nontrivial class implementation MyClass and its instance my:
>
> my = MyClass(args)
>
> MyClass uses in internals some variable which is not defined in MyClass
> itself. I want to extend instance of MyClass at runtime defining this
> variable and making new instance. It is like a class inheritance in a
> static way
I'm afraid I don't understand what you are asking. MyClass uses a
variable which is not defined in MyClass. Where is it defined? Is it a
global variable?
What do you mean, "like a class inheritance in a static way"?
Perhaps you should give an example of what you want to happen.
> class MyNewClass(MyClass):
> def __init__(s, a):
> s._variable = a
>
> but this doesn't give me ability to make inheritance at runtime of the
> single parent intance.
Why not?
What is the single parent instance?
> Finaly this should look like this
>
> my = MyClass(args)
>
> a1 = my.new(1)
> a2 = my.new(2)
>
> and e.t.c. Is it possible to release this interface in python?
I'm afraid none of this makes any sense to me. What does the new() method
do?
--
Steven
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:
class MyClass(object):
def __init__(s): pass
def set(s, key):
s.__key = key
def __str__(s):
return s.__key + ' ' + s.__other
def new(s, value):
return SubClass(s, value)
The problem is how to implement class SubClass which inherits MyClass,
define new variable __other accessible from MyClass intance and with
working application:
a = MyClass()
a.set('key1')
b1 = a.new('value1')
b2 = a.new('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?
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.
Redesigning the pair of classes, I get this:
class MyClass(object):
def __init__(self, key):
self.key = key # No obvious need to make key a private attribute.
def new(self, value):
return AnotherClass(self, value)
class AnotherClass(object):
def __init__(self, obj, value):
self.obj = obj
self.value = value
def __str__(self):
return "%s %s" % (self.obj.key, self.value)
which gives the behaviour you ask for:
>>> a = MyClass('key1')
key1 value1 , key1 value2
>>>
>>> a.key = 'key2'
>>> print b1, "," ,b2
key2 value1 , key2 value2
--
Steven
class Factory: # this is a Factory class
class MyClass: # the class you actually need
redFactory = Factory('red')
blueFactory = Factory('blue')
ex1 = redFactory.new('value1') # use the factory to return an instance
of MyClass initialized with the proper parameters
ex2 = blueFactory.new('value1')
print ex1
'red value1'
print ex2
'blue value1'
Is that want you want to do ? If so, I may elaborate a little more...
JM
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
else:
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.