Adding classes to modules at runtime from outside that module

Discussion in 'Python' started by frambooz@gmail.com, Apr 11, 2008.

  1. Guest

    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
     
    , Apr 11, 2008
    #1
    1. Advertising

  2. 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
     
    Andrew Warkentin, Apr 11, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 10, 8:05 pm, Andrew Warkentin <> wrote:
    > 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
     
    , Apr 11, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pradeep gummi
    Replies:
    1
    Views:
    939
    dhek bhun kho
    Sep 5, 2003
  2. Chris Widmer
    Replies:
    0
    Views:
    422
    Chris Widmer
    Mar 15, 2005
  3. Rim
    Replies:
    7
    Views:
    341
    Terry Reedy
    Jul 28, 2003
  4. Joshua J. Kugler

    Finding a module's sub modules at runtime

    Joshua J. Kugler, Mar 28, 2007, in forum: Python
    Replies:
    8
    Views:
    375
    Joshua J. Kugler
    Apr 3, 2007
  5. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    288
    Victor Bazarov
    Feb 7, 2012
Loading...

Share This Page