From: Alexander on
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

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)

a1 = my.new(1)
a2 = my.new(2)

and e.t.c. Is it possible to release this interface in python?


From: Steven D'Aprano on
On Sat, 17 Apr 2010 13:09:43 +0400, Alexander wrote:

> 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
From: Alexander on
On 17.04.2010 18:32, Steven D'Aprano wrote:
> On Sat, 17 Apr 2010 13:09:43 +0400, Alexander wrote:
>
>
>> 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.
>

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'


From: Steven D'Aprano on
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 = 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')
>>> b1 = a.new('value1')
>>> b2 = a.new('value2')
>>> print b1, "," ,b2
key1 value1 , key1 value2
>>>
>>> a.key = 'key2'
>>> print b1, "," ,b2
key2 value1 , key2 value2



--
Steven
From: Jean-Michel Pichavant on
Alexander wrote:
> On 17.04.2010 18:32, Steven D'Aprano wrote:
>
>> On Sat, 17 Apr 2010 13:09:43 +0400, Alexander wrote:
>>
>>
>>
>>> 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.
>>
>>
>
> 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'
>
>
>
Unfortunately I'm not sure you description clarifies anything.
My *guess* is that you would need a Factory class.

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