13

I'm learning python. I like to use help() or interinspect.getargspec to get information of functions in shell. But is there anyway I can get the argument/return type of function.

3
  • What's wrong with the help text? What's wrong with reading the code itself? Commented Feb 9, 2010 at 11:15
  • Of course it's wrong. Do you really like reading source code when you just want to call a method. Commented Feb 12, 2010 at 2:41
  • this article is telling about the Python 3 with the types of parameters in Python 3.x medium.com/@waqarnaeem2/parameters-in-python-1da9af59cbba Commented Apr 10, 2020 at 10:35

5 Answers 5

12

formatargspec is deprecated since version 3.5. Prefer signature

>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
...     pass

>>> sig = signature(foo)

>>> str(sig)
'(a, *, b:int, **kwargs)'

Note: Some callables may not be introspectable in certain implementations of Python. For example, in CPython, some built-in functions defined in C provide no metadata about their arguments.

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

2 Comments

Is there a way to get built-in types? Where do IDEs get built-in type infos from? I already suspect it's from .pyi files, and those are likely included with CPython, so how do I access them, ex. from an ipython shell?
All builtins variables can be listed from builtins module: import builtins; builtins.__dict__
9

In the 3.4.2 documentation https://docs.python.org/3/library/inspect.html, there is a mention of what you exactly need (namely getting the types of arguments to a function).

You will first need to define your function like this:

def f(a: int, b: float, c: dict, d: list, <and so on depending on number of parameters and their types>):

Then you can use formatargspec(*getfullargspec(f)) which returns a nice hash like this:

(a: int, b: float)

2 Comments

This question is from 2010...well before 3.4.2 existed. I'd consider removing this answer.
deprecated, use signature instead, see below.
5

If you mean during a certain call of the function, the function itself can get the types of its arguments by calling type on each of them (and will certainly know the type it returns).

If you mean from outside the function, no: the function can be called with arguments of any types -- some such calls will produce errors, but there's no way to know a priori which ones they will be.

Parameters can be optionally decorated in Python 3, and one possible use of such decoration is to express something about the parameters' types (and/or other constraints on them), but the language and standard library offer no guidance on how such decoration might be used. You might as well adopt a standard whereby such constraints are expressed in a structured way in the function's docstring, which would have the advantage of being applicable to any version of Python.

Comments

3

Best, shortest answer:

Use the inspect library with getfullargspec(function).annotations Note that you have to explicitly typecast the function arguments.

import inspect

def my_func(arg_1:str="One", arg_2:bool=False):
    pass

spec = inspect.getfullargspec(my_func).annotations

print(spec)

>>> {'arg_1': <class 'str'>, 'arg_2': <class 'bool'>}

From this you can use spec.values() to get the datatypes of each argument.

Comments

-4

There is a function called type().
Here are the docs

You can't tell in advance what type a function will return

>>> import random
>>> def f():
...  c=random.choice("IFSN")
...  if c=="I":
...   return 1
...  elif c=="F":
...   return 1.0
...  elif c=="S":
...   return '1'
...  return None
... 
>>> type(f())
<type 'float'>
>>> type(f())
<type 'NoneType'>
>>> type(f())
<type 'float'>
>>> type(f())
<type 'int'>
>>> type(f())
<type 'str'>
>>> type(f())
<type 'float'>
>>> type(f())
<type 'float'>
>>> type(f())
<type 'NoneType'>
>>> type(f())
<type 'str'>

It is usually good practise to only return one type of object from a function, but Python does not force that upon you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.