python-internals

Can someone explain how the source code of staticmethod works in python

懵懂的女人 提交于 2020-01-22 13:23:07
问题 First of all, I understand how, in general, a decorator work. And I know @staticmethod strips off the instance argument in the signature, making class C(object): @staticmethod def foo(): print 'foo' C.foo //<function foo at 0x10efd4050> C().foo //<function foo at 0x10efd4050> valid. However, I don't understand how the sourcec code of staticmethod make this happen. It seems to me that when wrapping method foo in staticmethod , an instance of staticmethod is instantiated, then some magic

Imports behave differently when in __init__.py that is imported

吃可爱长大的小学妹 提交于 2020-01-22 07:24:46
问题 Imports in an __init__.py seem to behave differently when the file is run, to when it is imported. If we have the following files: run.py : import test test/b.py : class B(object): pass test/__init__.py : from b import B print B print b If we run __init__.py we get an error as I expect: % python test/__init__.py <class 'b.B'> Traceback (most recent call last): File "test/__init__.py", line 6, in <module> print b NameError: name 'b' is not defined But if we run.py then we don't: % python run

Why does naive string concatenation become quadratic above a certain length?

我是研究僧i 提交于 2020-01-22 07:21:27
问题 Building a string through repeated string concatenation is an anti-pattern, but I'm still curious why its performance switches from linear to quadratic after string length exceeds approximately 10 ** 6: # this will take time linear in n with the optimization # and quadratic time without the optimization import time start = time.perf_counter() s = '' for i in range(n): s += 'a' total_time = time.perf_counter() - start time_per_iteration = total_time / n For example, on my machine (Windows 10,

Is the empty tuple in Python a “constant” [duplicate]

我是研究僧i 提交于 2020-01-21 13:47:35
问题 This question already has answers here : compare object to empty tuple with the 'is' operator in Python 2.x (4 answers) Closed 2 years ago . I want to make my code more (memory-)efficient. Right now we have a lot of functions that take an iterable as parameter like: def foo(para,meter,iterable): #... pass and sometimes we have to provide it an empty list to do its work properly: foo(14,25,[]) . The problem is that each time a new list is constructed: it requires to allocate on the heap, and a

Is the empty tuple in Python a “constant” [duplicate]

亡梦爱人 提交于 2020-01-21 13:43:19
问题 This question already has answers here : compare object to empty tuple with the 'is' operator in Python 2.x (4 answers) Closed 2 years ago . I want to make my code more (memory-)efficient. Right now we have a lot of functions that take an iterable as parameter like: def foo(para,meter,iterable): #... pass and sometimes we have to provide it an empty list to do its work properly: foo(14,25,[]) . The problem is that each time a new list is constructed: it requires to allocate on the heap, and a

Implementation of NoneType, Reasons and Details

六月ゝ 毕业季﹏ 提交于 2020-01-21 06:35:27
问题 I recently read somewhere that the special value None in python is a singleton object of its own class, specifically NoneType . This explained a lot, since most errors involving None in python produce AttributeError s instead of some special "NoneError" or something. Since all of these AttributeErrors reflected the attributes that NoneType lacked, I became intrigued by what attributes NoneType did have, if any. I decided to look into this NoneType and learn more about it. I've always found

Why is slice assignment faster than `list.insert`?

假如想象 提交于 2020-01-21 03:57:24
问题 Inspired by this nice answer, Here's a benchmark: import timeit def test1(): a = [1,2,3] a.insert(0,1) def test2(): a = [1,2,3] a[0:0]=[1] print (timeit.timeit('test1()','from __main__ import test1')) print (timeit.timeit('test2()','from __main__ import test2')) For me, test2 is sligtly faster (~10%). Why is that the case? I would expect it to be slower since: slice assignment must be able to accept iterables of any length and therefore must be more general. in slice assignment, we need to

Why does genexp(generator expression) is called genexp? not iterexp?

北城以北 提交于 2020-01-17 14:46:38
问题 A generator is a special kind of iterator, and it has some methods that an normal iterator doesn't have such as send() , close() ... etc. One can get a generator by using a genexp like below: g=(i for i in range(3)) the type of g will be a generator. But it seems weird to me that g is a generator because g.send will do nothing since g isn't returned by a function with yield keyword, and there would be no chance for one to catch the value passed by send method(not sure this is right), I don't

How to create a custom generator class that is correctly garbage collected

隐身守侯 提交于 2020-01-15 02:37:13
问题 I'm trying to write a class in Python that behaves as a generator object, particularly in that when it's garbage collected .close() is called on it. That's important because it means that when the generator is interrupted I can make sure it'll clean up after itself, for example closing files or releasing locks. Here's some explanatory code: If you interupt a generator, then when it's garbage collected, Python calls .close() on the generator object, which throws a GeneratorExit error into the

What happens when you inherent from a module instead of a class in Python?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-13 20:38:19
问题 I recently came across this question. import Object class Visitor(Object): def __init__(self): super(Visitor,self).__init__() def visit(self, obj): pass def getIsDone(self): return False isDone = property(fget =lambda self:self.getIsDone()) I get this error: TypeError: module.__init__() takes at most 2 arguments (3 given) and its answer: class A:pass print(A) #outputs <class '__main__.A'> import urllib print(urllib) #outputs <module 'urllib' from '/usr/lib/python3.2/urllib/__init__.py'> Your