python-internals

Which import mechanism is faster?

拈花ヽ惹草 提交于 2021-02-07 20:51:31
问题 1: %%timeit -n 1000000 import math math.sin(math.pi/2) 1000000 loops, best of 3: 284 ns per loop 2: %%timeit -n 1000000 from math import sin, pi sin(pi/2) 1000000 loops, best of 3: 1.01 µs per loop 回答1: There you are timing two different statements - and indeed, "common sense" has it that attribute lookup as in the first snippet should have a higher overhead than using local (in this case, global) scoped names. What is happening though, is that when one does from math import sin, pi , those

Why isn't __add__ implemented in Python's deque?

僤鯓⒐⒋嵵緔 提交于 2021-02-07 19:17:12
问题 Concatenating two deque s results in a TypeError . from collections import deque q = deque() q + q But __iadd__ is implemented so += is supported. q1 = deque([1]) q2 = deque([2]) q1 += q2 What is the reason that only __iadd__ get implemented? 回答1: This is a bug which is already fixed in the repos, so it should be included in the next released version of Python (3.5). 来源: https://stackoverflow.com/questions/32547316/why-isnt-add-implemented-in-pythons-deque

Why isn't __add__ implemented in Python's deque?

社会主义新天地 提交于 2021-02-07 19:04:57
问题 Concatenating two deque s results in a TypeError . from collections import deque q = deque() q + q But __iadd__ is implemented so += is supported. q1 = deque([1]) q2 = deque([2]) q1 += q2 What is the reason that only __iadd__ get implemented? 回答1: This is a bug which is already fixed in the repos, so it should be included in the next released version of Python (3.5). 来源: https://stackoverflow.com/questions/32547316/why-isnt-add-implemented-in-pythons-deque

Is there a difference between [] and list() when using id()?

我的未来我决定 提交于 2021-02-04 12:49:25
问题 Can somebody explain the following? Why is the id the same, but the lists are different? >>> [] is [] False >>> id([]) == id([]) True Is there difference in list creation? >>> id(list()) == id(list()) False >>> id([]) == id([]) True Why is this happening? I get two different lists. Why not only one, or three or more? >>> [].__repr__ <method-wrapper '__repr__' of list object at 0x7fd2be868128> >>> [].__repr__ <method-wrapper '__repr__' of list object at 0x7fd2be868170> >>> [].__repr__ <method

Will TextIOWrapper.tell reliably return zero at the beginning of a file?

会有一股神秘感。 提交于 2021-01-29 19:13:27
问题 The docs for TextIOBase.tell, from which TextIOWrapper inherits, state Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage. I am wondering, whether it will always return zero in the specific case that the file pointer is at the beginning of the file? I wrote a small, non-exhaustive, script to check some obvious scenarios, which returned zero for each case: import io import os rmodes = ['a', 'a+', 'r',

How does the Python for loop actually work?

我的未来我决定 提交于 2021-01-28 03:11:55
问题 I am curious to understand how Python for loops work under the hood. I tried to implement it somewhat like the following code snippet, is that how the for loop has been implemented? my_list = [1, 2, 3, 4, 5] # list itself is iterable but not iterator. Make it an iterator iter_list = iter(my_list) while True: try: print(next(iter_list)) except StopIteration: break 回答1: Yes, that's a good approximation of how the for loop construct is implemented. It certainly matches the for loop statement

tuples as function arguments

自闭症网瘾萝莉.ら 提交于 2021-01-26 17:58:50
问题 a tuple in python (in a code block) is defined by the commas; the parentheses are not mandatory (in the cases below). so these three are all equivalent: a, b = 1, 2 a, b = (1, 2) (a, b) = 1, 2 if i define a function def f(a, b): print(a, b) calling it this way will work: f(2, 3) this will not: f((2, 3)) # TypeError: f() missing 1 required positional argument: 'b' how does python treat tuples differently when they are function arguments? here the parentheses are necessary (i understand why

tuples as function arguments

梦想的初衷 提交于 2021-01-26 17:55:22
问题 a tuple in python (in a code block) is defined by the commas; the parentheses are not mandatory (in the cases below). so these three are all equivalent: a, b = 1, 2 a, b = (1, 2) (a, b) = 1, 2 if i define a function def f(a, b): print(a, b) calling it this way will work: f(2, 3) this will not: f((2, 3)) # TypeError: f() missing 1 required positional argument: 'b' how does python treat tuples differently when they are function arguments? here the parentheses are necessary (i understand why