質問

When I write a function in Python (v2.7), I very often have a type in mind for one of the arguments. I'm working with the unbelievably brilliant pandas library at the movemement, so my arguments are often 'intended' to be pandas.DataFrames.

In my favorite IDE (Spyder), when you type a period . a list of methods appear. Also, when you type the opening parenthesis of a method, the docstring appears in a little window.

But for these things to work, the IDE has to know what type a variable is. But of course, it never does. Am I missing something obvious about how to write Pythonic code (I've read Python Is Not Java but it doesn't mention this IDE autocomplete issue.

Any thoughts?

役に立ちましたか?

解決

I don't know if it works in Spyder, but many completion engines (e.g. Jedi) also support assertions to tell them what type a variable is. For example:

def foo(param):
    assert isinstance(param, str)
    # now param will be considered a str
    param.|capitalize
           center
           count
           decode
           ...

他のヒント

Actually I use IntelliJ idea ( aka pyCharm ) and they offer multiple ways to specify variable types:

1. Specify Simple Variable

Very simple: Just add a comment with the type information behind the definition. From now on Pycharm supports autcompletition! e.g.:

def route():
    json = request.get_json() # type: dict

Source: https://www.jetbrains.com/help/pycharm/type-hinting-in-pycharm.html

2. Specify Parameter:

Add three quote signs after the beginning of a method and the idea will autocomplete a docstring, as in the following example:

example of method parameters

Source: https://www.jetbrains.com/help/pycharm/using-docstrings-to-specify-types.html

(Currently on my mobile, going to make it pretty later)

If you're using Python 3, you can use function annotations. As an example:

@typechecked
def greet(name: str, age: int) -> str:
    print("Hello {0}, you are {1} years old".format(name, age))

I don't use Spyder, but I would assume there's a way for it to read the annotations and act appropriately.

I don't know whether Spyder reads docstrings, but PyDev does:

http://pydev.org/manual_adv_type_hints.html

So you can document the expected type in the docstring, e.g. as in:

def test(arg):
    'type arg: str'
    arg.<hit tab>

And you'll get the according string tab completion.

Similarly you can document the return-type of your functions, so that you can get tab-completion on foo for foo = someFunction().

At the same time, docstrings make auto-generated documention much more helpful.

The problem is with the dynamic features of Python, I use Spyder and I've used a lot more of python IDEs (PyCharm, IDLE, WingIDE, PyDev, ...) and ALL of them have the problem you stated here. So, when I want code completion for help I just instantiate the variable to the type I want and then type ".", for example: suppose you know your var df will be a DataFrame in some piece of code, you can do this df = DataFrame() and for now on the code completion should work for you just do not forget to delete (or comment) the line df = DataFrame() when you finish editing the code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top