python-internals

range non-default parameter follows default one

大城市里の小女人 提交于 2019-12-01 03:55:22
Why does range allow a non-default parameter ( stop ) to follow a default parameter ( start )? Case in point: >>> r = range(1, 2, 3) >>> print(r.start, r.stop, r.step) 1 2 3 >>> r = range(10) >>> print(r.start, r.stop, r.step) 0 10 1 Trying to emulate the signature is an obvious violation: def my_range(start=0, stop, end=1): pass I understand that the fact it is implemented in C probably allows for behavior that would be a violation in Pythonland. I'm guessing this was done to make the API more user-friendly but, I didn't find any sources to back it up (The source code doesn't tell much and

Implementation of NoneType, Reasons and Details

落花浮王杯 提交于 2019-12-01 03:33:16
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 the best way to learn about a new language feature is to use it, so I tried instantiating NoneType in

Internals for python tuples

走远了吗. 提交于 2019-12-01 03:29:56
>>> a=1 >>> b=1 >>> id(a) 140472563599848 >>> id(b) 140472563599848 >>> x=() >>> y=() >>> id(x) 4298207312 >>> id(y) 4298207312 >>> x1=(1) >>> x2=(1) >>> id(x1) 140472563599848 >>> id(x2) 140472563599848 until this point I was thinking there will be only one copy of immutable object and that will be shared(pointed) by all the variables. But when I tried, the below steps I understood that I was wrong. >>> x1=(1,5) >>> y1=(1,5) >>> id(x1) 4299267248 >>> id(y1) 4299267320 can anyone please explain me the internals? thefourtheye >>> x1=(1) >>> x2=(1) is actually the same as >>> x1=1 >>> x2=1 In

Why do new style class and old style class have different behavior in this case?

非 Y 不嫁゛ 提交于 2019-12-01 03:13:22
I found something interesting, here is a snippet of code: class A(object): def __init__(self): print "A init" def __del__(self): print "A del" class B(object): a = A() If I run this code, I will get: A init But if I change class B(object) to class B() , I will get: A init A del I found a note in the __del__ doc : It is not guaranteed that del () methods are called for objects that still exist when the interpreter exits. Then, I guess it's because that B.a is still referenced(referenced by class B ) when the interpreter exists. So, I added a del B before the interpreter exists manually, and

Why are assignments not allowed in Python's `lambda` expressions?

只谈情不闲聊 提交于 2019-12-01 03:08:39
This is not a duplicate of Assignment inside lambda expression in Python , i.e., I'm not asking how to trick Python into assigning in a lambda expression. I have some λ-calculus background. Considering the following code, it looks like Python is quite willing to perform side-effects in lambda expressions: #!/usr/bin/python def applyTo42(f): return f(42) def double(x): return x * 2 class ContainsVal: def __init__(self, v): self.v = v def store(self, v): self.v = v def main(): print('== functional, no side effects') print('-- print the double of 42') print(applyTo42(double)) print('-- print 1000

Why do new style class and old style class have different behavior in this case?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 23:06:29
问题 I found something interesting, here is a snippet of code: class A(object): def __init__(self): print "A init" def __del__(self): print "A del" class B(object): a = A() If I run this code, I will get: A init But if I change class B(object) to class B() , I will get: A init A del I found a note in the __del__ doc: It is not guaranteed that del () methods are called for objects that still exist when the interpreter exits. Then, I guess it's because that B.a is still referenced(referenced by

In what structure is a Python object stored in memory?

穿精又带淫゛_ 提交于 2019-11-30 19:44:08
Say I have a class A: class A(object): def __init__(self, x): self.x = x def __str__(self): return self.x And I use sys.getsizeof to see how many bytes instance of A takes: >>> sys.getsizeof(A(1)) 64 >>> sys.getsizeof(A('a')) 64 >>> sys.getsizeof(A('aaa')) 64 As illustrated in the experiment above, the size of an A object is the same no matter what self.x is. So I wonder how python store an object internally? It depends on what kind of object, and also which Python implementation :-) In CPython, which is what most people use when they use python , all Python objects are represented by a C

How are variables names stored and mapped internally?

邮差的信 提交于 2019-11-30 18:19:49
I read https://stackoverflow.com/a/19721096/1661745 and it seems that in CPython, variables are simply names that are associated with references. There are several things going on with the statement x=5: an int object with the value of 5 is created (or found if it already exists) the name x is created (or disassociated with the last object 'x' labeled) the reference count to the new (or found) int object is increased by 1 the name x is associated with the object with the value '5' created (or found). However, I'm still not clear with exactly how variables are implemented internally. Namely:

What's the difference between a reversed tuple and a reversed list?

帅比萌擦擦* 提交于 2019-11-30 17:19:40
Reversing a tuple and reversing a list returns objects of different type: >>> reversed((1,2)) <reversed at 0x7fffe802f748> >>> reversed([1,2]) <list_reverseiterator at 0x7fffebdd4400> They have the same dir . Neither type is a subclass of the other. Why is that? What can one do that the other can't? jsbueno Basically, a list implements the __reversed__ method and returns an specialized object, while tuple falls back to the default implementation of reversed for any sequence: >>> list.__reversed__ <method '__reversed__' of 'list' objects> >>> tuple.__reversed__ AttributeError: type object

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

血红的双手。 提交于 2019-11-30 17:12:47
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 create a new list on the right hand side just to get it to work. Can anybody help me understand this?