classobj?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venky

    #1

    classobj?

    Hi,

    I am trying to create classes at runtime based on input from a textfile.
    I am trying to use the function new.classobj. I am able to create the
    new classes successfully, however I fail to understand on how to add
    this new class to the current dictionary.

    cl = new.classobj('S omeClass', (BaseClass, ), {})

    After this call, how do I instantiate SomeClass?

    I understand cl() will instantiate this, however this defeats my
    purpose, since the name of the class is obtained at runtime.

    Thanks

    venky
  • attn.steven.kuo@gmail.com

    #2
    Re: classobj?

    On Feb 26, 5:43 pm, Venky <v...@nospam.co mwrote:
    Hi,
    >
    I am trying to create classes at runtime based on input from a textfile.
    I am trying to use the function new.classobj. I am able to create the
    new classes successfully, however I fail to understand on how to add
    this new class to the current dictionary.
    >
    cl = new.classobj('S omeClass', (BaseClass, ), {})
    >
    After this call, how do I instantiate SomeClass?
    >
    I understand cl() will instantiate this, however this defeats my
    purpose, since the name of the class is obtained at runtime.
    >

    Do you mean that you want to add it to globals()?

    globals()['SomeClass'] = cl

    myinst = SomeClass()
    print isinstance(myin st, SomeClass)
    print isinstance(myin st, BaseClass)

    --
    Hope this helps,
    Steven


    Comment

    • Venky

      #3
      Re: classobj?

      ....
      >
      Do you mean that you want to add it to globals()?
      >
      globals()['SomeClass'] = cl
      >
      myinst = SomeClass()
      print isinstance(myin st, SomeClass)
      print isinstance(myin st, BaseClass)
      >
      --
      Hope this helps,
      Steven
      >
      >
      >
      Thanks. That's what I was looking for.

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: classobj?

        In <Xns98E3DE8104C 0Fvenkynospamco m@216.77.188.18 >, Venky wrote:
        >Do you mean that you want to add it to globals()?
        >>
        >globals()['SomeClass'] = cl
        >>
        >myinst = SomeClass()
        >print isinstance(myin st, SomeClass)
        >print isinstance(myin st, BaseClass)
        >>
        >
        Thanks. That's what I was looking for.
        Probably not because now you have to know the name when you write the
        static source code. You said the names are discovered at runtime, so how
        do you know it's `SomeClass`? Put you classes into a dictionary with the
        name as key.

        `globals()` is a dictionary too, but it is more work to get an object by
        name from it (globals()['SomeClass'] vs. some_dict['SomeClass']) and you
        don't risk that some of your dynamically created classes overwrites an
        existing one with the same name.

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        Working...