Showing posts with label Python-inspect-module. Show all posts
Showing posts with label Python-inspect-module. Show all posts

Monday, January 30, 2017

Finding the arity of a Python function

By Vasudev Ram

While browsing through some Python code, I had the idea of trying to find the arity of a Python function. The arity of a function is the number of arguments it takes.

Digression: I had first come across the concept of arity when reading some books on functional or logic programming, years ago. E.g. Prolog has the concept of arity, IIRC. Just did a quick search for the phrase prolog arity and found a few results confirming this. Here is one, from cse.unsw.edu.au:

arity (link 1)

But in Prolog, the word arity is related to a functor (link 2), not a function, according to the page at link 2.

Excerpt from link 1:

[ The arity of a functor is the number of arguments it takes. For example, the arity of likes/2, as in likes(jane, pizza), is 2, as it takes two arguments, jane and pizza. ]

In fact the use of likes/2 gives a clue to the fact that the Erlang language was influenced by Prolog (and I've read that the first version of Erlang was written in Prolog), because a function foo with arity 2 in Erlang is written as foo/2, as for the Prolog functor above.

[ Note: I'm not an expert in either Prolog or Erlang; these are just casual observations. Anyone who knows better, feel free to comment / correct. ]

Anyway, end of digression.

Here is the code I came up with to find the arity of a Python function. I've used the isfunction and getargspec functions of the inspect module.
'''
function_arity.py
Purpose: To find the arity of a Python function.
Author: Vasudev Ram
Copyright 2017 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: https://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
'''

import inspect

# Define a few functions with increasing arity:

def f0():
    pass

def f1(a1):
    pass

def f2(a1, a2):
    pass

def f3(a1, a2, a3):
    pass

def f4(a1, a2, a3, a4):
    pass

def main():

    # Define a few non-function objects:
    int1 = 0
    float1 = 0.0 
    str1 = ''
    tup1 = ()
    lis1 = []

    # Test the function arity-finding code with both the functions 
    # and the non-function objects:
    for o in (f0, f1, f2, f3, f4, int1, float1, str1, tup1, lis1):
        if not inspect.isfunction(o):
            print repr(o), 'is not a function'
            continue
        n_args = len(inspect.getargspec(o)[0])
        if n_args == 0:
            num_suffix = '(no) args'
        elif n_args == 1:
            num_suffix = 'arg'
        else:
            num_suffix = 'args'
        print o.__name__, 'is a function that takes', \
            n_args, num_suffix
    
if __name__ == '__main__':
    main()
And here is the output when I run the program:
$ python function_arity.py
f0 is a function that takes 0 (no) args
f1 is a function that takes 1 arg
f2 is a function that takes 2 args
f3 is a function that takes 3 args
f4 is a function that takes 4 args
0 is not a function
0.0 is not a function
'' is not a function
() is not a function
[] is not a function

I also did a web search:

https://www.google.com/search?q=find+the+arity+of+python+function

in which one hit showed another way of doing it (using gettattr). This StackOverflow question:

How to find out the arity of a method in Python

had some answers; the ones by Python guru Alex Martelli (the martellibot) and Anurag Uniyal were interesting.

Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Managed WordPress Hosting by FlyWheel



Saturday, November 30, 2013

Using inspect.getargvalues to debug Python programs

By Vasudev Ram


I was looking through the Python docs recently, for ideas for any other useful debugging techniques, some time after I wrote this post about a simple Python debugging function.

Though I initially tried something else, I later came up with this technique, which may be helpful to debug Python programs. It uses the getargvalues function of Python's inspect module.

Here is a test program showing the use of inspect.getargvalues(); note that you have to pass the return value of inspect.currentframe() to inspect.getargvalues():
# test_getargvalues.py
# Author: Vasudev Ram - http://dancingbison.com

from debug1 import debug1
import inspect

def foo(arg1, arg2=None):
    print "In foo()"
    a = 1
    b = "2"
    c = True
    d = [ 3, "4" ]
    e = { 5: "five", "six": 6 }
    argvalues = inspect.getargvalues(inspect.currentframe())
    debug1("argvalues.args", argvalues.args)
    debug1("argvalues.varargs", argvalues.varargs)
    debug1("argvalues.keywords", argvalues.keywords)
    debug1("argvalues.locals", argvalues.locals)
    debug1("locals()", locals())

def bar(arg1, arg2, *args, **kwds):
    print "In bar()"
    argvalues = inspect.getargvalues(inspect.currentframe())
    debug1("argvalues.args", argvalues.args)
    debug1("argvalues.varargs", argvalues.varargs)
    debug1("argvalues.keywords", argvalues.keywords)
    debug1("argvalues.locals", argvalues.locals)
    debug1("locals()", locals())

def main():
    foo(1, 2)
    bar(1, 2, 3, 4, five=5, six=6)

main()
Run the program with:

python test_getargvalues.py

Here is its output:
In foo()
argvalues.args : ['arg1', 'arg2']
argvalues.varargs : None
argvalues.keywords : None
argvalues.locals : {'a': 1, 'c': True, 'b': '2', 'e': {'six': 6, 5: 'five'}, 'd': [3, '4'], 'arg1': 1, 'arg2': 2}
locals() : {'a': 1, 'c': True, 'b': '2', 'e': {'six': 6, 5: 'five'}, 'd': [3, '4'], 'arg1': 1, 'arg2': 2, 'argvalues': ArgInfo(args=['arg1', 'arg2'], varargs=None, keywords=None, locals={...})}
In bar()
argvalues.args : ['arg1', 'arg2']
argvalues.varargs : 'args'
argvalues.keywords : 'kwds'
argvalues.locals : {'arg1': 1, 'arg2': 2, 'args': (3, 4), 'kwds': {'six': 6, 'five': 5}}
locals() : {'arg1': 1, 'arg2': 2, 'args': (3, 4), 'kwds': {'six': 6, 'five': 5}, 'argvalues': ArgInfo(args=['arg1', 'arg2'], varargs='args', keywords='kwds', locals={...})}
Note that for comparison, I also printed the value of the Python built-in locals(), and found that the output of locals() is almost the same as, but a subset, of the output of getargvalues() - at least when the function which you are debugging has varargs and keyword arguments.

Read other posts about Python on jugad2.
- Vasudev Ram - Dancing Bison Enterprises
Contact Page


O'Reilly 50% Ebook Deal of the Day


Saturday, May 18, 2013

Python's inspect module is powerful

27.13. inspect — Inspect live objects — Python v2.7.5 documentation

The inspect module comes as part of the standard library of Python. It allows you to inspect live objects at run time by using its methods.

Here is an example:

import inspect
class Bar( object):
    def foo( self ):
        print "in Bar.foo 4"
        self .a = 1
        self .di = { 'b' : 2, 'c' : 3 }
        self .li = [  4, 5, 6 ]

bar = Bar()
bar.foo()
for member in inspect.getmembers(bar):
    print member

Running the above code gives this as (partial) output:

in Bar.foo 4

('__dict__', {'a': 1, 'li': [4, 5, 6], 'di': {'c': 3, 'b': 2}})
('__doc__', None)
('a', 1)
('di', {'c': 3, 'b': 2})
('foo', bound method Bar.foo of __main__.Bar object at 0x4035bfac)
('li', [4, 5, 6])

This shows both the bound methods and the member variables of the instance bar.

The inspect module can do a lot of other things too. Check the docs for it, linked at the top of this post.

You can also modify and run the above code snippet at this codepad.org URL:

http://codepad.org/dMLmkhQ6

It may not last there for too long, though, since they probably delete pastes to make room for new ones.

- Vasudev Ram
dancingbison.com