python-internals

Importing a py file within itself

旧巷老猫 提交于 2020-01-10 05:29:06
问题 This is test.py: import sys a = 50 b = [1,2] def change(): print "Here 1" import test print "Here 2" test.a = -1 test.b = [0,1] return def main(): print "Here 3" change() print "Here 4" print a, b if 1: main() The above python code when ran on system generates the following output: Here 3 Here 1 Here 3 Here 1 Here 2 Here 4 -1 [0, 1] Here 2 Here 4 50 [1, 2] What I am confused why is not there an infinite loop of "Here 1 \n Here 3" outputs. How can the print a, b outputs can be justified? 回答1:

Python source code for built-in “in” operator

核能气质少年 提交于 2020-01-09 18:38:31
问题 I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation? My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have. 回答1: To find the implementation of any python operator, first

Python source code for built-in “in” operator

雨燕双飞 提交于 2020-01-09 18:37:05
问题 I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation? My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have. 回答1: To find the implementation of any python operator, first

Python source code for built-in “in” operator

余生长醉 提交于 2020-01-09 18:35:14
问题 I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation? My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have. 回答1: To find the implementation of any python operator, first

Accessing the list while being sorted

冷暖自知 提交于 2020-01-08 16:34:48
问题 Can I access a list while it is being sorted in the list.sort() b = ['b', 'e', 'f', 'd', 'c', 'g', 'a'] f = 'check this' def m(i): print i, b, f return None b.sort(key=m) print b this returns b [] check this e [] check this f [] check this d [] check this c [] check this g [] check this a [] check this Note that individual items of list b is sent to function m . But at m the list b is empty, however it can see the variable f , which has same scope as list b . Why does function m print b as []

Accessing the list while being sorted

怎甘沉沦 提交于 2020-01-08 16:34:06
问题 Can I access a list while it is being sorted in the list.sort() b = ['b', 'e', 'f', 'd', 'c', 'g', 'a'] f = 'check this' def m(i): print i, b, f return None b.sort(key=m) print b this returns b [] check this e [] check this f [] check this d [] check this c [] check this g [] check this a [] check this Note that individual items of list b is sent to function m . But at m the list b is empty, however it can see the variable f , which has same scope as list b . Why does function m print b as []

How does CPython determine whether a user supplied an optional argument?

我是研究僧i 提交于 2020-01-07 08:08:31
问题 I started wondering how CPython can tell the difference between None as a default argument and None as a specified argument. For example, dict.pop() will throw a KeyError if the key doesn't exist. .pop() also takes an argument for a default return value. In this example, we could set the default return to None , so: some_dict.pop(some_key, None) If you were to define pop in Python, you'd probably have: def pop(some_key, default=None): In that case, it wouldn't make a difference whether you

How does Python distinguish explicitly passed None as argument in built-ins

ε祈祈猫儿з 提交于 2020-01-04 02:34:26
问题 I experimented with the next code: >>> f = object() # It's obvious behavior: >>> f.foo Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute 'foo' # However, the next one is surprising me! >>> getattr(f, 'foo') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute 'foo' # And this one returns None as expected: >>> getattr(f, 'foo', None) Then I found this pseudo

Why are f-strings faster than str() to parse values?

心已入冬 提交于 2020-01-03 08:28:12
问题 I was playing around with f-strings, (see PEP 498), and I decided to check the speed of the f-string parse, (e.g. f"{1}") in comparison with the usual str parse (e.g str(1)). But for my surprise, when I checked the velocity of both methods with the timeit function, I found out that >>> from timeit import timeit >>> timeit("f'{1}'") 0.1678762999999961 whereas >>> timeit("str(1)") 0.3216999999999999 or even the repr func, which in most of the cases is faster than str cast >>> timeit("repr(1)")

Why do Python emit STORE_SUBSTR when there's already an INPLACE_ADD?

和自甴很熟 提交于 2020-01-03 03:00:32
问题 If you disassemble the following function def test(): t = (1, 2, [30]) t[2] += [40] return t You'll see that the corresponding bytecode for t[2] += [40] looks like this: 3 18 LOAD_FAST 0 (t) 21 LOAD_CONST 2 (2) 24 DUP_TOPX 2 27 BINARY_SUBSCR 28 LOAD_CONST 4 (40) 31 BUILD_LIST 1 34 INPLACE_ADD 35 ROT_THREE 36 STORE_SUBSCR [40] is concatenated to the list stored in t[2] after INPLACE_ADD , why does Python decide to add a STORE_SUBSCR anyway? 回答1: This is because INPLACE_ADD only requests that