Get types of arguments in python
Asked Answered
I

5

12

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.

Interracial answered 9/2, 2010 at 6:11 Comment(3)
What's wrong with the help text? What's wrong with reading the code itself?Sontich
Of course it's wrong. Do you really like reading source code when you just want to call a method.Interracial
this article is telling about the Python 3 with the types of parameters in Python 3.x medium.com/@waqarnaeem2/parameters-in-python-1da9af59cbbaViehmann
J
11

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.

Joselow answered 5/1, 2022 at 17:30 Comment(2)
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?Jagannath
All builtins variables can be listed from builtins module: import builtins; builtins.__dict__Joselow
P
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)
Pix answered 6/11, 2014 at 12:32 Comment(2)
This question is from 2010...well before 3.4.2 existed. I'd consider removing this answer.Collocate
deprecated, use signature instead, see below.Iroquois
C
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.

Cassareep answered 9/2, 2010 at 6:16 Comment(0)
S
1

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.

Stormy answered 6/10, 2023 at 2:53 Comment(0)
S
-3

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

Sardius answered 9/2, 2010 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.