-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.

6
  • You can use an editor/IDE that takes advantage of type-hinting for code-completion. I believe PyCharm does. I don't know about liclipse. Commented Jun 22, 2017 at 21:30
  • 1
    If it's always an instance of a then why don't you make functionX a method? Commented Jun 22, 2017 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. Commented Jun 22, 2017 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. Commented Jun 22, 2017 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. :) Commented Jun 23, 2017 at 16:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.