473,569 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object inheritance

I am trying to implement some kind of object inheritance. Just like
one class can extend from another, I want to do the same on objects
dynamically.

I just thought that I can share my excitement here.

Suppose there are classes A and B and their instances a and b.

class A:
def foo(self): self.say('foo')
def say(self, msg):
print 'a.say', msg

class B:
def say(self, msg):
print 'b.say', msg

a = A()
b = B()

I want b to inherit the behavior of a.
>>b.extend_from (a)
b.foo()
b.say foo

I looked around and found that some people talked about similar ideas,
but didn't find any concrete implementation.

I came up with the following implementation using meta-classes.

class ExtendMetaClass (type):
def __init__(cls, *a, **kw):
# take all attributes except special ones
keys = [k for k in cls.__dict__.ke ys() if not
k.startswith('_ _')]
d = [(k, getattr(cls, k)) for k in keys]

# remove those attibutes from class
for k in keys:
delattr(cls, k)

# remember then as dict _d
cls._d = dict(d)

def curry(f, arg1):
def g(*a, **kw):
return f(arg1, *a, **kw)
g.__name__ = f.__name__
return g

def _getattr(self, name):
"""Get value of attribute from self or super."""
if name in self.__dict__:
return self.__dict__[name]
elif name in self._d:
value = self._d[name]
if isinstance(valu e, types.MethodTyp e):
return curry(value, self)
else:
return value
else:
if self._super != None:
return self._super._ge tattr(name)
else:
raise AttributeError, name

def __getattr__(sel f, name):
"""Returns value of the attribute from the sub object.
If there is no sub object, self._getattr is called.
"""
if name.startswith ('super_'):
return self._super._ge tattr(name[len('super_'):])

if self._sub is not None:
return getattr(self._s ub, name)
else:
return self._getattr(n ame)

def extend_from(sel f, super):
"""Makes self extend from super.
"""
self._super = super
super._sub = self

cls.__getattr__ = __getattr__
cls._getattr = _getattr
cls._super = None
cls._sub = None
cls.extend_from = extend_from

class Extend:
__metaclass__ = ExtendMetaClass
def __init__(self, super=None):
if super:
self.extend_fro m(super)

And the above example becomes:

class A(Extend):
def foo(self): self.say('foo')
def say(self, msg):
print 'a.say', msg

class B(Extend):
def say(self, msg):
print 'b.say', msg
# self.super_foo calls foo method on the super object
self.super_say( 'super ' + msg)

a = A()
b = B()
>>b.extend_from (a)
b.foo()
b.say foo
a.say super foo

There are one issue with this approach. Once b extends from a,
behavior of a also changes, which probably should not. But that
doesn't hurt me much.

Any comments?

Oct 26 '07 #1
8 1485
Can you tell any specific use case for doing this?

Regards,
Pradeep

On 10/26/07, Anand <an********@gma il.comwrote:
I am trying to implement some kind of object inheritance. Just like
one class can extend from another, I want to do the same on objects
dynamically.

I just thought that I can share my excitement here.

Suppose there are classes A and B and their instances a and b.

class A:
def foo(self): self.say('foo')
def say(self, msg):
print 'a.say', msg

class B:
def say(self, msg):
print 'b.say', msg

a = A()
b = B()

I want b to inherit the behavior of a.
>b.extend_from( a)
b.foo()
b.say foo

I looked around and found that some people talked about similar ideas,
but didn't find any concrete implementation.

I came up with the following implementation using meta-classes.

class ExtendMetaClass (type):
def __init__(cls, *a, **kw):
# take all attributes except special ones
keys = [k for k in cls.__dict__.ke ys() if not
k.startswith('_ _')]
d = [(k, getattr(cls, k)) for k in keys]

# remove those attibutes from class
for k in keys:
delattr(cls, k)

# remember then as dict _d
cls._d = dict(d)

def curry(f, arg1):
def g(*a, **kw):
return f(arg1, *a, **kw)
g.__name__ = f.__name__
return g

def _getattr(self, name):
"""Get value of attribute from self or super."""
if name in self.__dict__:
return self.__dict__[name]
elif name in self._d:
value = self._d[name]
if isinstance(valu e, types.MethodTyp e):
return curry(value, self)
else:
return value
else:
if self._super != None:
return self._super._ge tattr(name)
else:
raise AttributeError, name

def __getattr__(sel f, name):
"""Returns value of the attribute from the sub object.
If there is no sub object, self._getattr is called.
"""
if name.startswith ('super_'):
return self._super._ge tattr(name[len('super_'):])

if self._sub is not None:
return getattr(self._s ub, name)
else:
return self._getattr(n ame)

def extend_from(sel f, super):
"""Makes self extend from super.
"""
self._super = super
super._sub = self

cls.__getattr__ = __getattr__
cls._getattr = _getattr
cls._super = None
cls._sub = None
cls.extend_from = extend_from

class Extend:
__metaclass__ = ExtendMetaClass
def __init__(self, super=None):
if super:
self.extend_fro m(super)

And the above example becomes:

class A(Extend):
def foo(self): self.say('foo')
def say(self, msg):
print 'a.say', msg

class B(Extend):
def say(self, msg):
print 'b.say', msg
# self.super_foo calls foo method on the super object
self.super_say( 'super ' + msg)

a = A()
b = B()
>b.extend_from( a)
b.foo()
b.say foo
a.say super foo

There are one issue with this approach. Once b extends from a,
behavior of a also changes, which probably should not. But that
doesn't hurt me much.

Any comments?

--
http://mail.python.org/mailman/listinfo/python-list
Oct 26 '07 #2
On Oct 26, 5:31 pm, "Pradeep Jindal" <praddyjin...@g mail.comwrote:
Can you tell any specific use case for doing this?
I have many implementaions of a db interface.

SimpleDB - simple implementation
BetterDB - optimized implementation
CachedDB - an implementation with caching of queries
RestrictedDB - implementation with permissions

Now, I want to combine these implementations and use.
Typical use case scenarios are:

db = RestrictedDB(Ca chedDB(SimpleDB ()))
db = RestrictedDB(Si mpleDB())
db = RestrictedDB(Be tterDB())
db = RestrictedDB(Ca chedDB(BetterDB ())
db = CachedDB(Simple DB())
etc..

Oct 26 '07 #3
On Friday 26 Oct 2007 6:21:57 pm Anand wrote:
On Oct 26, 5:31 pm, "Pradeep Jindal" <praddyjin...@g mail.comwrote:
Can you tell any specific use case for doing this?

I have many implementaions of a db interface.

SimpleDB - simple implementation
BetterDB - optimized implementation
CachedDB - an implementation with caching of queries
RestrictedDB - implementation with permissions

Now, I want to combine these implementations and use.
Typical use case scenarios are:

db = RestrictedDB(Ca chedDB(SimpleDB ()))
db = RestrictedDB(Si mpleDB())
db = RestrictedDB(Be tterDB())
db = RestrictedDB(Ca chedDB(BetterDB ())
db = CachedDB(Simple DB())
etc..
I agree with Duncan. According to me, this should be called Delegation rather
than inheritance. And delegation should work without any conflicts of
identifier names and all that. I think, it should be all about several
objects implementing a protocol (interface) and that should work cleanly.
Oct 27 '07 #4
On Oct 28, 1:16 am, Pradeep Jindal <praddyjin...@g mail.comwrote:
On Friday 26 Oct 2007 6:21:57 pm Anand wrote:
On Oct 26, 5:31 pm, "Pradeep Jindal" <praddyjin...@g mail.comwrote:
Can you tell any specific use case for doing this?
I have many implementaions of a db interface.
SimpleDB - simple implementation
BetterDB - optimized implementation
CachedDB - an implementation with caching of queries
RestrictedDB - implementation with permissions
Now, I want to combine these implementations and use.
Typical use case scenarios are:
db = RestrictedDB(Ca chedDB(SimpleDB ()))
db = RestrictedDB(Si mpleDB())
db = RestrictedDB(Be tterDB())
db = RestrictedDB(Ca chedDB(BetterDB ())
db = CachedDB(Simple DB())
etc..

I agree with Duncan. According to me, this should be called Delegation rather
than inheritance. And delegation should work without any conflicts of
identifier names and all that. I think, it should be all about several
objects implementing a protocol (interface) and that should work cleanly.
I don't think so. It is not as simple as delegation.

In the example that I gave previously, call to self.say in A.foo
calls B.say not A.say, which I think is not possible with delegation.


Oct 31 '07 #5
En Wed, 31 Oct 2007 00:36:34 -0300, Anand <an********@gma il.comescribió:
>No, that is an argument for multiple-inheritance, mixin classes etc. You
know when constructing the object what behaviour you want it to have. It
isn't an argument for changing the behaviour of an existing object
dynamically.
Partially true. I don't want to change the behavior of an exiting
object. I know what behavior I want to have at the time of creating.
This can be achieved by creating classes dynamically by parameterizing
the base class, which I thought is ugly.
If you know at compile time which features you want, you can use multiple
inheritance to define the new class.
If you only know the set of clases to mix at run time, you can dinamically
create new classes with 'type':

newclass = type('newclass' , (CachedDB, SimpleDB), {})
db = newclass()
db.withIDs will call CachedDB.withID , as you want.

[In another message]
In the example that I gave previously, call to self.say in A.foo
calls B.say not A.say, which I think is not possible with delegation.
It's easy to create a new type inheriting from your classes:

obj = type('', (B,A), {})()
obj.foo()
should print: b.say "foo"

--
Gabriel Genellina

Oct 31 '07 #6
On Oct 31, 8:26 am, Anand <anandol...@gma il.comwrote:
On Oct 28, 1:16 am, Pradeep Jindal <praddyjin...@g mail.comwrote:
On Friday 26 Oct 2007 6:21:57 pm Anand wrote:
On Oct 26, 5:31 pm, "Pradeep Jindal" <praddyjin...@g mail.comwrote:
Can you tell any specific use case for doing this?
I have many implementaions of a db interface.
SimpleDB - simple implementation
BetterDB - optimized implementation
CachedDB - an implementation with caching of queries
RestrictedDB - implementation with permissions
Now, I want to combine these implementations and use.
Typical use case scenarios are:
db = RestrictedDB(Ca chedDB(SimpleDB ()))
db = RestrictedDB(Si mpleDB())
db = RestrictedDB(Be tterDB())
db = RestrictedDB(Ca chedDB(BetterDB ())
db = CachedDB(Simple DB())
etc..
I agree with Duncan. According to me, this should be called Delegation rather
than inheritance. And delegation should work without any conflicts of
identifier names and all that. I think, it should be all about several
objects implementing a protocol (interface) and that should work cleanly.

I don't think so. It is not as simple as delegation.

In the example that I gave previously, call to self.say in A.foo
calls B.say not A.say, which I think is not possible with delegation.
Let me explain what I meant, its all about several objects (e.g. "a")
implementing a protocol, in your case the "say" method, and some
object (e.g. "b") extending those objects by adding some more
functionality but requiring that those objects, that it extends from,
follow a specific protocol ("a" must have say method). Also note that,
"B" or "b" is partial implementation of what its meant for, I mean,
calling "b"'s "say" will not work if "b" has not yet extended from "a"
or any other object implementing the same protocol.

Less time, can't get into more details. Bye for now.

Thanks
-Pradeep

Oct 31 '07 #7
On Oct 31, 9:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Wed, 31 Oct 2007 00:36:34 -0300, Anand <anandol...@gma il.comescribió:
No, that is an argument for multiple-inheritance, mixin classes etc. You
know when constructing the object what behaviour you want it to have. It
isn't an argument for changing the behaviour of an existing object
dynamically.
Partially true. I don't want to change the behavior of an exiting
object. I know what behavior I want to have at the time of creating.
This can be achieved by creating classes dynamically by parameterizing
the base class, which I thought is ugly.

If you know at compile time which features you want, you can use multiple
inheritance to define the new class.
If you only know the set of clases to mix at run time, you can dinamically
create new classes with 'type':

newclass = type('newclass' , (CachedDB, SimpleDB), {})
db = newclass()
db.withIDs will call CachedDB.withID , as you want.
Interesting.

CachedDB.withID should call withID from its super class (here
SimpleDB), when it is not in cache. How to do that?
Oct 31 '07 #8
En Wed, 31 Oct 2007 19:26:17 -0300, Anand <an********@gma il.comescribió:
On Oct 31, 9:57 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>>
If you know at compile time which features you want, you can use
multiple
inheritance to define the new class.
If you only know the set of clases to mix at run time, you can
dinamically
create new classes with 'type':

Interesting.

CachedDB.withID should call withID from its super class (here
SimpleDB), when it is not in cache. How to do that?
Using super(). See
<http://docs.python.org/dev/tutorial/classes.html#mu ltiple-inheritance>
and <http://www.python.org/2.3/mro.htmlfor the details.

This is a running version of your example:

pyclass SimpleDB(object ):
.... def withID(self, id):
.... print "SimpleDB.withI D",id
.... return "Data for %s" % id
.... def withIDs(self, ids):
.... return [self.withID(id) for id in ids]
....
pyclass CachedDB(object ):
.... def __init__(self):
.... # CachedDB instances must have a cache dictionary
.... super(CachedDB, self).__init__( self)
.... self.cache = {}
.... def withID(self, id):
.... print "CachedDB.withI D",id
.... res = self.cache.get( id)
.... if res is None:
.... print "cache miss!"
.... self.cache[id] = res = super(CachedDB, self).wit
hID(id)
.... return res
....
pynewclass = type('newclass' , (CachedDB, SimpleDB), {})
pydb = newclass()
pyprint db.withIDs([1,2,1,3,2,1])
CachedDB.withID 1
cache miss!
SimpleDB.withID 1
CachedDB.withID 2
cache miss!
SimpleDB.withID 2
CachedDB.withID 1
CachedDB.withID 3
cache miss!
SimpleDB.withID 3
CachedDB.withID 2
CachedDB.withID 1
['Data for 1', 'Data for 2', 'Data for 1', 'Data for 3', 'Data f
or 2', 'Data for 1']

Multiple inheritance is not the only option. You may want to read about
decorators too: instead of overriding the method withID, you can decorate
it with some variant of the "memoize" decorator. Look for the (excellent!)
article on decorators by Michele Simionato.

--
Gabriel Genellina

Nov 1 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2592
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
6
7951
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a...
14
2377
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface? thanks, craig buchanan
8
3969
by: Raterus | last post by:
Hi, I don't even know if this can be done.. Lets say I have two objects, --- Public Class Employee End Class
3
6174
by: Robert Abi Saab | last post by:
Hi everyone. I just finished a course on PostgreSQL and I found out that PostgreSQL doesn't provide any object relational features (as claimed in the official documentation), except table inheritance and very limited user defined types (I defined a UDT with 2 attributes and couldn't use it in a table, and the trainer said it must contain 1...
73
4416
by: Water Cooler v2 | last post by:
I am new to PHP, just one day new. But I am otherwise a seasoned programmer on many other languages like C, VB 6, C#, VB.NET, Win32 platform and have some tiny bit experience in MFC, C++, Python. All the tutorials I read so far about PHP contain procedural examples. I haven't yet come accross a tutorial on the object oriented PHP. Could...
23
1925
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj = NewClass() newobj.__dict__.update(object.__dict__) return newobj
6
1862
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance" using the prototype property. I realize there are no classes in JS, that code therefore lives in objects instead of class definitions, and that...
3
3568
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: Ajax.Base = function() {}; Ajax.Base.prototype = { setOptions: function(options) { <...>
3
1437
by: Rahul | last post by:
While reading Inside the C++ object model I came through the following paragraph Point3d origin, *ptr = &origin; A) origin.x = 0.0; B) ptr->x = 0.0; The book says "A & B statements performs equivalently if x is a member of a struct, class, single inheritance hierarchy, or multiple inheritance hierarchy" This is because compiler knows...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.