0

after spending hours in googling, I learned a lot, but did not find any close topic to the following:

I am currently programming on dynamically created classes via a factory method. The reason is, that the method names should be populated according to a separate yaml-file. It comes down to this problem:

def create_class_with_dynamic_fuction_name(fun_name):
    def fun(self):
        print "Hello, I am fun"

    class X:
        pass

    setattr(X, fun_name, fun)
    return X

Kls1= create_class_with_dynamic_fuction_name('my_function_name')
kls1= Kls1()
kls1.my_function_name()

This code works. However, if you have this code in the editor of sypder (aka. Spider IDE, spyderlib), the autocomplete will not show my_function_name in its context box. Neither is it possible to inspect it via Ctrl+I for easy retrieval of the docstring-help.

The same problem arises, if a class is created with type(classname, (), clsdict). In that case, one only finds mro inside the autocomplete-context-menu.

Also I found, that it is not a problem of the closure pattern above. You can do

def create_class_with_static_function_name():
    def fun(self):
        print "Hello, I am fun"

    class X:
        my_function_name = fun

    return X

Kls2= create_class_with_static_function_name()
kls2= Kls2()
kls2.my_function_name()

In such case, the my_function_name shows correctly up, but it is not what I aimed for.

Would be glad to here any suggestions to accomplish dynamic creation of classes with dynamic method names, which are correctly handled in spyder's autocompletion within the editor window.

PyPhreak
  • 3
  • 1
  • 2

1 Answers1

0

(Spyder dev here) Sorry, it seems the completion library we use (called rope) can't do this kind of completions on dynamic classes and attributes.

You're welcome to ask on its mailing list about it, and if there's something we can do in our side, we'll be happy to help.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
  • Thanks for your answer. It also came clear to me, that rope cannot provide this kind of functionality. Would be interesting idea though, if the editor could be optionally hooked to the current interpreter, such that the interpreter's context menues are being used instead. Just an idea on the side. – PyPhreak Jul 08 '14 at 09:11
  • I think [Canopy](https://www.enthought.com/products/canopy/), an environment similar to Spyder, works this way. Maybe you can take a look at it. – Carlos Cordoba Jul 08 '14 at 13:33