Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 08-03-2012, 08:48 PM
Tobiah
Guest
 
Posts: n/a
Default Deciding inheritance at instantiation?

I have a bunch of classes from another library (the html helpers
from web2py). There are certain methods that I'd like to add to
every one of them. So I'd like to put those methods in a class,
and pass the parent at the time of instantiation. Web2py has
a FORM class for instance. I'd like to go:

my_element = html_factory(FORM)

Then my_element would be an instance of my class, and also
a child of FORM.

I started messing with decorators, but it became difficult
for me to visualise how to do this.

Thanks!

Toby

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 08-03-2012, 09:55 PM
Terry Reedy
Guest
 
Posts: n/a
Default Re: Deciding inheritance at instantiation?

On 8/3/2012 4:48 PM, Tobiah wrote:
> I have a bunch of classes from another library (the html helpers
> from web2py). There are certain methods that I'd like to add to
> every one of them. So I'd like to put those methods in a class,
> and pass the parent at the time of instantiation. Web2py has
> a FORM class for instance. I'd like to go:
>
> my_element = html_factory(FORM)
>
> Then my_element would be an instance of my class, and also
> a child of FORM.
>
> I started messing with decorators, but it became difficult
> for me to visualise how to do this.


Use type(name, bases, content) for dynamic class creation.

--
Terry Jan Reedy

Reply With Quote
  #3 (permalink)  
Old 08-03-2012, 11:52 PM
Nobody
Guest
 
Posts: n/a
Default Re: Deciding inheritance at instantiation?

On Fri, 03 Aug 2012 13:48:08 -0700, Tobiah wrote:

> I have a bunch of classes from another library (the html helpers
> from web2py). There are certain methods that I'd like to add to
> every one of them. So I'd like to put those methods in a class,
> and pass the parent at the time of instantiation. Web2py has
> a FORM class for instance. I'd like to go:
>
> my_element = html_factory(FORM)
>
> Then my_element would be an instance of my class, and also
> a child of FORM.


You can use type() to create classes dynamically. E.g.:

class my_base_class(object):
# extra methods

subclasses = {}

def html_factory(cls, *args, **kwargs):
name = "my_" + cls.__name__
if name not in subclasses:
subclasses[name] = type(name, (cls, my_base_class), {})
return subclasses[name](*args, **kwargs)

Reply With Quote
  #4 (permalink)  
Old 08-04-2012, 03:14 AM
Steven W. Orr
Guest
 
Posts: n/a
Default Re: Deciding inheritance at instantiation?

On 8/3/2012 4:48 PM, Tobiah wrote:
> I have a bunch of classes from another library (the html helpers
> from web2py). There are certain methods that I'd like to add to
> every one of them. So I'd like to put those methods in a class,
> and pass the parent at the time of instantiation. Web2py has
> a FORM class for instance. I'd like to go:
>
> my_element = html_factory(FORM)
>
> Then my_element would be an instance of my class, and also
> a child of FORM.
>
> I started messing with decorators, but it became difficult
> for me to visualise how to do this.
>
> Thanks!
>
> Toby


Your class inherits from whatever is in the class statement.

class Foo(object):
pass

Here, Foo inherits from object, but you can replace object with any tuple of
classes which can be redefined before instantiation.

class Base1(object):
pass

class Base2(object):
pass

Now we can define Foo2 to inherit from something that better be a tuple of
classes at instantiation time.

class Foo2(bases):
pass

bases = (Base1,)

foo2 = Foo2() # foo2 is a Foo2 which inherits from Base1.

bases = (Base1, Bace2)

foob1b2 = Foo2() # foob1b2 is a Foo2 which inherits from Base1 and Base2.

Who was it who said: "Give a man a shovel and he'll dig himself one helluva hole"?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Reply With Quote
  #5 (permalink)  
Old 08-06-2012, 05:42 PM
Tobiah
Guest
 
Posts: n/a
Default Re: Deciding inheritance at instantiation?

On 08/03/2012 02:55 PM, Terry Reedy wrote:
> On 8/3/2012 4:48 PM, Tobiah wrote:
>> I have a bunch of classes from another library (the html helpers
>> from web2py). There are certain methods that I'd like to add to
>> every one of them. So I'd like to put those methods in a class,
>> and pass the parent at the time of instantiation. Web2py has
>> a FORM class for instance. I'd like to go:
>>
>> my_element = html_factory(FORM)
>>
>> Then my_element would be an instance of my class, and also
>> a child of FORM.
>>
>> I started messing with decorators, but it became difficult
>> for me to visualise how to do this.

>
> Use type(name, bases, content) for dynamic class creation.
>


Very cool. Just what I was after. Thanks.

Tobiah
Reply With Quote
  #6 (permalink)  
Old 08-07-2012, 02:53 AM
alex23
Guest
 
Posts: n/a
Default Re: Deciding inheritance at instantiation?

On Aug 4, 6:48*am, Tobiah <t...@tobiah.org> wrote:
> I have a bunch of classes from another library (the html helpers
> from web2py). *There are certain methods that I'd like to add to
> every one of them. *So I'd like to put those methods in a class,
> and pass the parent at the time of instantiation. *Web2py has
> a FORM class for instance. *I'd like to go:
>
> * * * * my_element = html_factory(FORM)
>
> Then my_element would be an instance of my class, and also
> a child of FORM.


I've lately begun to prefer composition over inheritance for
situations like this:

class MyElementFormAdapter(object):
def __init__(self, form):
self.form = form

def render_form(self):
self.form.render()

my_element = MyElementFormAdapter(FORM)
my_element.render_form()
my_element.form.method_on_form()

Advantages include being more simple and obvious than multiple
inheritance, and avoiding namespace clashes:

class A(object):
def foo(self):
print 'a'

class B(object):
def foo(self):
print 'b'

class InheritFromAB(A, B):
pass

class AdaptAB(object):
def __init__(self, a, b):
self.a = a
self.b = b

>>> inherit = InheritFromAB()
>>> inherit.foo()

a
>>> adapt = AdaptAB(A(), B())
>>> adapt.a.foo()

a
>>> adapt.b.foo()

b
Reply With Quote
  #7 (permalink)  
Old 08-07-2012, 05:52 PM
Tobiah
Guest
 
Posts: n/a
Default Re: Deciding inheritance at instantiation?

Interesting stuff. Thanks.

On 08/06/2012 07:53 PM, alex23 wrote:
> On Aug 4, 6:48 am, Tobiah<t...@tobiah.org> wrote:
>> I have a bunch of classes from another library (the html helpers
>> from web2py). There are certain methods that I'd like to add to
>> every one of them. So I'd like to put those methods in a class,
>> and pass the parent at the time of instantiation. Web2py has
>> a FORM class for instance. I'd like to go:
>>
>> my_element = html_factory(FORM)
>>
>> Then my_element would be an instance of my class, and also
>> a child of FORM.

>
> I've lately begun to prefer composition over inheritance for
> situations like this:
>
> class MyElementFormAdapter(object):
> def __init__(self, form):
> self.form = form
>
> def render_form(self):
> self.form.render()
>
> my_element = MyElementFormAdapter(FORM)
> my_element.render_form()
> my_element.form.method_on_form()
>
> Advantages include being more simple and obvious than multiple
> inheritance, and avoiding namespace clashes:
>
> class A(object):
> def foo(self):
> print 'a'
>
> class B(object):
> def foo(self):
> print 'b'
>
> class InheritFromAB(A, B):
> pass
>
> class AdaptAB(object):
> def __init__(self, a, b):
> self.a = a
> self.b = b
>
> >>> inherit = InheritFromAB()
> >>> inherit.foo()

> a
> >>> adapt = AdaptAB(A(), B())
> >>> adapt.a.foo()

> a
> >>> adapt.b.foo()

> b


Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:30 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.