-2

I am trying to figure out the solution for the following problem:

#ExampleA.py
class a:
    def my_great_method_A(self):
        pass

#ExampleB.py
def functionX(inst_a): #Argument 'inst_a' will be always ExampleA.py's class a.
    inst_a.my_great_method_A() #<--- 

I use Liclipse as a python editor. When I am typing the last line, "a.my_gr...", I want to have the editor's auto filling feature kicks in to suggest to use "my_great_method_A()". However, it actually does not suggest anything.

I understand why, because the editor doesn't have any clue if 'inst_a' is class 'a'. To deal with this issue, I could do the following to make the autofiller work:

#ExampleA.py
class a:
    def my_great_method_A(self):
        pass

#ExampleB.py
import ExampleA

def functionX(inst_a): #Argument 'inst_a' will be always ExampleA.py's class a.
    ExampleA.a.my_great_method_A(inst_a) #<--- then autofilling works

However, for the code's readability, I would rather use the . format and I believe everyone the same way. But I do not know how everyone deals with this. Many times I have to go into the imported file and copy & paste the method name, which is tedious. Obviously I am missing something that everyone is aware of. By the way this is my first time to post on stackoverflow. I hope this is a valid thing to pose here.

  • You can use an editor/IDE that takes advantage of [type-hinting](https://www.python.org/dev/peps/pep-0484/) for code-completion. I believe [PyCharm](https://blog.jetbrains.com/pycharm/2015/11/python-3-5-type-hinting-in-pycharm-5/) does. I don't know about liclipse. – juanpa.arrivillaga Jun 22 '17 at 21:30
  • 1
    If it's always an instance of `a` then why don't you make `functionX` a method? – MSeifert Jun 22 '17 at 21:33
  • @MSeifert, you can think of this as an over-simplified case. I think there are plenty of cases to use a class method inside of a function. – Shuhei Kazuma Jun 22 '17 at 21:44
  • You need to stop wanting your editor to be clairvoyant. How can this editor, or any editor, possibly know that a variable named inst_a is always an instance of some class named `a`? `class a` doesn't even have to be defined in the same file, or defined before you type in the body of the function. You can write words in a comment to that effect, but editors today are sadly lacking in the ability to parse and understand the true meaning of such things. – Paul Cornelius Jun 22 '17 at 21:56
  • @PaulCornelius "You can write words in a comment to that effect" . That is what I was looking for. If that is what people generally do, it is all I need. :) – Shuhei Kazuma Jun 23 '17 at 16:40
  • This is an aspect of Python that differs from many other languages. Variable names in Python don't have a type; type information resides only in the objects to which the names are bound at run-time. One tool I use to help overcome this is "pylint". It has a type-inference system that parses your source code and works amazingly well. It can often tell you when you are accessing a function or a member variable that doesn't exist. I rely on it heavily. Warning: it's not easy to set up and use effectively but it's worth the effort. – Paul Cornelius Jun 23 '17 at 21:42

2 Answers2

0

I don't know of a way to make this editor guess the type you're expecting. But since Python is untyped it will always only be guessing.

However notice your workaround of using the class explicit method is not a good practice. It will not allow you to pass extensions of ExampleA in the future in case your code will evolve some day. So it's more than just readability

Shmulik Asafi
  • 203
  • 1
  • 6
  • You are right about extensions. Actually I am passing class a's extension object to the function so my example#2 would not work. I just put that just to make auto-filling work. – Shuhei Kazuma Jun 22 '17 at 21:56
0

LiClipse/PyDev can recognize type hints in docstrings (as explained in http://www.pydev.org/manual_adv_type_hints.html) or using the new PEP 484 type hints (https://www.python.org/dev/peps/pep-0484/)... So, if you use one of those, it should work.

Note: I personally like docstrings better, but it's probably a matter of taste and both should be recognizable by LiClipse/PyDev.

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78