python-internals

Why doesn't Python optimize away temporary variables?

徘徊边缘 提交于 2019-12-05 16:49:21
Fowler's Extract Variable refactoring method, formerly Introduce Explaining Variable , says use a temporary variable to make code clearer for humans. The idea is to elucidate complex code by introducing an otherwise unneeded local variable, and naming that variable for exposition purposes. It also advocates this kind of explaining over comments.. Other languages optimize away temporary variables so there's no cost in time or space resources. Why doesn't Python do this? In [3]: def multiple_of_six_fat(n): ...: multiple_of_two = n%2 == 0 ...: multiple_of_three = n%3 == 0 ...: return multiple_of

Can I speedup an iterable class when I know it's length in advance?

大憨熊 提交于 2019-12-05 16:19:41
PEP 424 mentions in the "Rationale" that: Being able to pre-allocate lists based on the expected size, as estimated by __length_hint__ , can be a significant optimization. CPython has been observed to run some code faster than PyPy, purely because of this optimization being present. So I asked myself the question that I'm now asking here: Is it possible to speed up some iterable class processing an iterator (when it's possible to correctly predict it's "length") based on this knowledge? Setting aside the generator/iterator terminology confusion, the __length_hint__ method is a really minor

How to get reference count of a PyObject?

让人想犯罪 __ 提交于 2019-12-05 14:32:09
问题 How to get reference count of a PyObject from C++? There are functions Py_INCREF and Py_DECREF which increase/decrease it, but I haven't found any function which return object's reference count. I need it for debugging purposes. 回答1: The reference count of each and every object is stored in the PyObject itself, in a variable called ob_refcnt. You can directly access that. typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; # Reference count struct _typeobject *ob_type; }

Embedding Python in C: Error in linking - undefined reference to PyString_AsString

扶醉桌前 提交于 2019-12-05 10:48:59
I am trying to embed a python program inside a C program. My OS is Ubuntu 14.04 I try to embed python 2.7 and python 3.4 interpreter in the same C code base (as separate applications). The compilation and linking works when embedding python 2.7 but not for the python 3.4. It fails during the linker stage. Here is my C code (just an example not real code) simple.c #include <stdio.h> #include <Python.h> int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pFunc, *pValue; char module[] = "get_version"; char func[] = "get_version"; char module_path[] = "."; Py_Initialize(); PyObject *sys

How does Python bypass normal attribute lookup to find `__dict__`?

徘徊边缘 提交于 2019-12-05 08:14:42
问题 I understand that __dict__ in obj.__dict__ is a descriptor attribute of type(obj) , so the lookup for obj.__dict__ is type(obj).__dict__['__dict__'].__get__(obj) . From https://stackoverflow.com/a/46576009 It's tempting to say that __dict__ has to be a descriptor because implementing it as a __dict__ entry would require you to find the __dict__ before you can find the __dict__ , but Python already bypasses normal attribute lookup to find __dict__ when looking up other attributes , so that's

How int() object uses “==” operator without __eq__() method in python2?

会有一股神秘感。 提交于 2019-12-05 01:03:21
Recently I read the "Fluent python" and understood how == operator works with python objects, using __eq__() method. But how it works with int instances in python2? >>> a = 1 >>> b = 1 >>> a == b True >>> a.__eq__(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute '__eq__' in python3 all a.__eq__(b) returns True Blender Python prefers to use rich comparison functions ( __eq__ , __lt__ , __ne__ , etc.), but if those don't exist, it falls back to using a single comparison function ( __cmp__ , removed in Python 3): These are the

List lookup faster than tuple?

蓝咒 提交于 2019-12-04 23:56:25
In the past, when I've needed array-like indexical lookups in a tight loop, I usually use tuples, since they seem to be generally extremely performant (close to using just n-number of variables). However, I decided to question that assumption today and came up with some surprising results: In [102]: l = range(1000) In [103]: t = tuple(range(1000)) In [107]: timeit(lambda : l[500], number = 10000000) Out[107]: 2.465047836303711 In [108]: timeit(lambda : t[500], number = 10000000) Out[108]: 2.8896381855010986 Tuple lookups appear to take 17% longer than list lookups! Repeated experimentation

Unexpected Behavior of itertools.groupby

这一生的挚爱 提交于 2019-12-04 22:55:04
问题 This is the observed behavior: In [4]: x = itertools.groupby(range(10), lambda x: True) In [5]: y = next(x) In [6]: next(x) --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-6-5e4e57af3a97> in <module>() ----> 1 next(x) StopIteration: In [7]: y Out[7]: (True, <itertools._grouper at 0x10a672e80>) In [8]: list(y[1]) Out[8]: [9] The expected output of list(y[1]) is [0,1,2,3,4,5,6,7,8,9] What's going on here?

Why does it take longer to import a function from a module than the entire module itself?

有些话、适合烂在心里 提交于 2019-12-04 22:19:41
Consider: >>> timeit.timeit('from win32com.client import Dispatch', number=100000) 0.18883283882571789 >>> timeit.timeit('import win32com.client', number=100000) 0.1275979248277963 It takes significantly longer to import only the Dispatch function rather than the entire module, which seems counter intuitive. Could someone explain why the overhead for taking a single function is so bad? Thanks! That's because: from win32com.client import Dispatch is equivalent to: import win32com.client #import the whole module first Dispatch = win32com.client.Dispatch #assign the required attributes to global

In-place custom object unpacking different behavior with __getitem__ python 3.5 vs python 3.6

对着背影说爱祢 提交于 2019-12-04 21:24:09
问题 a follow-up question on this question: i ran the code below on python 3.5 and python 3.6 - with very different results: class Container: KEYS = ('a', 'b', 'c') def __init__(self, a=None, b=None, c=None): self.a = a self.b = b self.c = c def keys(self): return Container.KEYS def __getitem__(self, key): if key not in Container.KEYS: raise KeyError(key) return getattr(self, key) def __str__(self): # python 3.6 # return f'{self.__class__.__name__}(a={self.a}, b={self.b}, c={self.c})' # python 3.5