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.
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.
.pyi
files, and those are likely included with CPython, so how do I access them, ex. from an ipython
shell? –
Jagannath import builtins; builtins.__dict__
–
Joselow 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)
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.
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.
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
© 2022 - 2024 — McMap. All rights reserved.
help
text? What's wrong with reading the code itself? – Sontich