Skip to content

Instantly share code, notes, and snippets.

@ruoyu0088
Created October 4, 2016 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruoyu0088/d00e3a8970ce457175e49b17893157c2 to your computer and use it in GitHub Desktop.
Save ruoyu0088/d00e3a8970ce457175e49b17893157c2 to your computer and use it in GitHub Desktop.
class A:
def __init__(self):
self.x = 1
self.y = 2
def get_x(self):
return self.x
def get_y(self):
return self.y
a = A()
%timeit a.x
%timeit a.get_x()
%timeit getattr(a, "x", None)
"""
output:
10000000 loops, best of 3: 150 ns per loop
1000000 loops, best of 3: 598 ns per loop
1000000 loops, best of 3: 388 ns per loop
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment