generating method names 'dynamically'

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

    #1

    generating method names 'dynamically'

    Is it possible to have method names of a class generated somehow dynamically?

    More precisely, at the time of writing a program that contains a class
    definition I don't know what the names of its callable methods should
    be. I have entries stored in a database that are changing from time to
    time and I always want to be able to call (from another program) those
    method names which are at the moment present in the database.
  • Farshid Lashkari

    #2
    Re: generating method names 'dynamically'

    > Is it possible to have method names of a class generated somehow dynamically?

    If you don't know the name of the method ahead of time, how do you write
    the code for it? Do you use some dummy name? If so, once you have the
    name determined then you could use setattr to set the method name.
    Here's a simple example:

    class MyClass:
    def DummyMethodName (self): pass

    setattr(MyClass ,'NewMethodName ',MyClass.Dummy MethodName)

    -Farshid

    Comment

    • Murali

      #3
      Re: generating method names 'dynamically'

      import inspect

      x = ABC() # create an instance of class ABC
      print inspect.getmemb ers(x,inspect.i smethod)
      --------------------

      Most of any introspection stuff can be done using the module "inspect".

      Comment

      • Murali

        #4
        Re: generating method names 'dynamically'

        x.__class__.__d ict__[mname](x,*args,**kwar gs)

        here
        x is an instance of a class FOO
        FOO has a method "bar" (if the value of mname is "bar")
        args is a tuple whose length is the number of positional arguments
        accepted by bar
        kwargs is a dictionary corresponding to the keyword arguments accepted
        by bar.

        Hope this answers your questions.

        - Murali

        Comment

        • bruno at modulix

          #5
          Re: generating method names 'dynamically'

          Daniel Nogradi wrote:[color=blue]
          > Is it possible to have method names of a class generated somehow dynamically?[/color]
          [color=blue][color=green][color=darkred]
          >>> class Dummy(object):[/color][/color][/color]
          .... pass
          ....[color=blue][color=green][color=darkred]
          >>> def mymethod(self, *args, **kw):[/color][/color][/color]
          .... pass
          ....[color=blue][color=green][color=darkred]
          >>> setattr(Dummy, 'a_dynamically_ generated_metho d_name', mymethod)
          >>>
          >>> Dummy.a_dynamic ally_generated_ method_name[/color][/color][/color]
          <unbound method Dummy.mymethod>[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]


          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          Working...