A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/308728.html below:

"optimizing out" getattr

"optimizing out" getattr "optimizing out" getattrDaishi Harada daishi at gmail.com
Wed Sep 14 22:46:44 EDT 2005
Hi,

I'd like to get the 'get2' function below to
perform like the 'get1' function (I've included
timeit.py results).

I'm not sure how to write 'mkget' to do achieve
this, however, except to use 'exec' - is that what
would be necessary?

Thanks in advance,
d

---
class A:
    a = 1
    b = 2
a = A()
labels = ('a', 'b')
def get1(x):
    return (x.a, x.b)
def mkget(attrs):
    def getter(x):
        return tuple(getattr(x, label) for label in attrs)
    return getter
get2 = mkget(labels)

# % timeit.py -s "import test" "test.get1(test.a)"
# 1000000 loops, best of 3: 0.966 usec per loop
# % timeit.py -s "import test" "test.get2(test.a)"
# 100000 loops, best of 3: 4.46 usec per loop
---


More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4