Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Adding classes to modules at runtime from outside that module

Reply
Thread Tools

Adding classes to modules at runtime from outside that module

 
 
frambooz@gmail.com
Guest
Posts: n/a
 
      04-10-2008
Hey guys,

In Python, is it possible to add classes to a module at run-time?

Say I have a module foo and a module bar. Foo has class A and B, and
bar has class C. I want to add class C to foo so I can access it as
foo.C, but i want to do it without modifying foo's source.

Is this at all possible?

Thanks much.

` Rogier van Etten
 
Reply With Quote
 
 
 
 
Andrew Warkentin
Guest
Posts: n/a
 
      04-11-2008
wrote:

> In Python, is it possible to add classes to a module at run-time?
>
> Say I have a module foo and a module bar. Foo has class A and B, and
>bar has class C. I want to add class C to foo so I can access it as
>foo.C, but i want to do it without modifying foo's source.
>
> Is this at all possible?
>
>

Yes.

You would do something like

import foo
import bar

foo.C = bar.C
 
Reply With Quote
 
 
 
 
frambooz@gmail.com
Guest
Posts: n/a
 
      04-11-2008
On Apr 10, 8:05*pm, Andrew Warkentin <andr...@datanet.ab.ca> wrote:
> framb...@gmail.com wrote:
> > *In Python, is it possible to add classes to a module at run-time?

>
> > *Say I have a module foo and a module bar. Foo has class A and B, and
> >bar has class C. I want to add class C to foo so I can access it as
> >foo.C, but i want to do it without modifying foo's source.

>
> > *Is this at all possible?

>
> Yes.
>
> You would do something like
>
> import foo
> import bar
>
> foo.C = bar.C


Wow. That was a lot easier than expected. Thanks, all.

` Rogier
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
help with pix inside->outside + dmz->outside + inside->outside->dmz Jack Cisco 0 09-19-2007 01:57 AM
Finding a module's sub modules at runtime Joshua J. Kugler Python 8 04-03-2007 07:42 PM
nat from outside to outside dt1649651@yahoo.com Cisco 1 08-21-2005 04:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57