Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26462
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!reader01.nrc01.news.zen.net.uk.POSTED!not-for-mail |
|---|---|
| From | Nobody <nobody@nowhere.com> |
| Subject | Re: Deciding inheritance at instantiation? |
| Date | Sat, 04 Aug 2012 00:52:53 +0100 |
| User-Agent | Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) |
| Message-Id | <pan.2012.08.03.23.53.04.844000@nowhere.com> |
| Newsgroups | comp.lang.python |
| References | <dOWSr.44251$Zl3.41187@newsfe06.iad> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Lines | 26 |
| Organization | Zen Internet |
| NNTP-Posting-Host | 5f187f43.news.zen.co.uk |
| X-Trace | DXC=UOnk@9:XX:8FiQn`d_;:_;a0UP_O8AJo<=dR0\ckLKG0WeZ<[7LZNR6;^>bE\k^nk8M2Z^cWRFGA;1kQbCBbWkM? |
| X-Complaints-To | abuse@zen.co.uk |
| Xref | csiph.com comp.lang.python:26462 |
Show key headers only | View raw
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)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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