leon
leon

Reputation: 443

Get types of arguments in python

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.

Upvotes: 13

Views: 18901

Answers (5)

yetixhunting
yetixhunting

Reputation: 41

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.

Upvotes: 2

Antiez
Antiez

Reputation: 957

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.

Upvotes: 11

Venky
Venky

Reputation: 131

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)

Upvotes: 9

John La Rooy
John La Rooy

Reputation: 304503

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

Upvotes: -4

Alex Martelli
Alex Martelli

Reputation: 882851

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.

Upvotes: 5

Related Questions