class instance customization
Alexander
b3nder at yandex.ru
Sat Apr 17 11:55:44 EDT 2010
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'
More information about the Python-list
mailing list