Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26469

Re: Deciding inheritance at instantiation?

Date 2012-08-03 23:14 -0400
From "Steven W. Orr" <steveo@syslang.net>
Organization SysLang, Inc.
Subject Re: Deciding inheritance at instantiation?
References <dOWSr.44251$Zl3.41187@newsfe06.iad>
Newsgroups comp.lang.python
Message-ID <mailman.2923.1344050059.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Deciding inheritance at instantiation? Tobiah <toby@tobiah.org> - 2012-08-03 13:48 -0700
  Re: Deciding inheritance at instantiation? Terry Reedy <tjreedy@udel.edu> - 2012-08-03 17:55 -0400
    Re: Deciding inheritance at instantiation? Tobiah <toby@tobiah.org> - 2012-08-06 10:42 -0700
  Re: Deciding inheritance at instantiation? Nobody <nobody@nowhere.com> - 2012-08-04 00:52 +0100
  Re: Deciding inheritance at instantiation? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-04 01:14 +0000
  Re: Deciding inheritance at instantiation? "Steven W. Orr" <steveo@syslang.net> - 2012-08-03 23:14 -0400
  Re: Deciding inheritance at instantiation? alex23 <wuwei23@gmail.com> - 2012-08-06 19:53 -0700
    Re: Deciding inheritance at instantiation? Tobiah <toby@tobiah.org> - 2012-08-07 10:52 -0700

csiph-web