class instance customization

A

Alexander

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?
 
S

Steven D'Aprano

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?
 
A

Alexander

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'
 
S

Steven D'Aprano

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:
key2 value1 , key2 value2
 
J

Jean-Michel Pichavant

Alexander said:
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
 
A

Alexander

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?
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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top