python-internals

Why is range(0) == range(2, 2, 2) True in Python 3?

末鹿安然 提交于 2020-07-14 16:39:12
问题 Why do ranges which are initialized with different values compare equal to one another in Python 3? When I execute the following commands in my interpreter: >>> r1 = range(0) >>> r2 = range(2, 2, 2) >>> r1 == r2 True The result is True . Why is this so? Why are two different range objects with different parameter values treated as equal? 回答1: The range objects are special: Python will compare range objects as Sequences . What that essentially means is that the comparison doesn't evaluate how

Why is range(0) == range(2, 2, 2) True in Python 3?

六月ゝ 毕业季﹏ 提交于 2020-07-14 16:38:34
问题 Why do ranges which are initialized with different values compare equal to one another in Python 3? When I execute the following commands in my interpreter: >>> r1 = range(0) >>> r2 = range(2, 2, 2) >>> r1 == r2 True The result is True . Why is this so? Why are two different range objects with different parameter values treated as equal? 回答1: The range objects are special: Python will compare range objects as Sequences . What that essentially means is that the comparison doesn't evaluate how

Why python set displays in “same” order if sets are unordered?

旧街凉风 提交于 2020-07-01 14:38:41
问题 I'm taking a first look at the python language from Python wikibook. For sets the following is mentioned - We can also have a loop move over each of the items in a set. However, since sets are unordered, it is undefined which order the iteration will follow. and the code example given is s = set("blerg") for letter in s: print letter Output : r b e l g When I run the program I get the results in the same order, no matter how many times I run. If sets are unordered and order of iteration is

How to call parent class init with default values from child class?

时光总嘲笑我的痴心妄想 提交于 2020-06-16 16:10:32
问题 I am trying to give my subclasses default variables and stop duplicating code as the file is getting fat: class Base(object): def __init__(self, username=None, password=None, start_url=None): self.username = username self.password = password self.start_url = start_url class Subclass(Base): def __init__(self, username="hoss_it87", password="whatsgoodSO", start_url="www.boss-sauce.com"): super(Subclass, self).__init__() Base works of course, but I want Subclass to init the same way, just

what are count0, count1 and count2 values returned by the Python gc.get_count()

依然范特西╮ 提交于 2020-06-15 04:13:29
问题 The documentation for python's gc package says this about gc.get_count(): gc.get_count() Return the current collection counts as a tuple of (count0, count1, count2). Here is a sample program: import gc if __name__=="__main__": print("making some data") for k in range(10): root = [range(i,1000) for i in range(1,1000)] print("len(gc.get_objects):",len(gc.get_objects())) print("gc.get_stats:",gc.get_stats()) print("gc.get_count:",gc.get_count()) Here is the output: making some data len(gc.get

Why doesn't Python have a “__req__” (reflected equality) method?

心不动则不痛 提交于 2020-06-14 06:16:47
问题 I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: >>> arr = np.array([1,2,3,4,5]) >>> arr == AnyOf(2,3) np.array([False, True, True, False, False]) without having to use a list comprehension (as in np.array(x in (2,3) for x in arr ). (I maintain a UI that lets (trusted) users type in arbitrary code, and a == AnyOf(1,2,3) is a lot more palatable than a list

Why doesn't Python have a “__req__” (reflected equality) method?

浪子不回头ぞ 提交于 2020-06-14 06:16:06
问题 I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: >>> arr = np.array([1,2,3,4,5]) >>> arr == AnyOf(2,3) np.array([False, True, True, False, False]) without having to use a list comprehension (as in np.array(x in (2,3) for x in arr ). (I maintain a UI that lets (trusted) users type in arbitrary code, and a == AnyOf(1,2,3) is a lot more palatable than a list

How is variable assignment implemented in CPython?

心已入冬 提交于 2020-06-07 04:39:05
问题 I know that variables in Python are really just references/pointers to some underlying object(s). And since they're pointers, I guess they somehow "store" or are otherwise associated with the address of the objects they refer to. Such an "address storage" probably happens at a low level in the CPython implementation. But my knowledge of C isn't good enough to infer this from the source code, nor do I know where in the source to begin looking. So, my question is: In the implementation of

Changing a class attribute within __init__

北城余情 提交于 2020-05-29 04:19:30
问题 I was looking at the Stack Overflow question Counting instances of a class?, and I'm not sure why that solution works and one using simple addition doesn't. I guess this is more of a question of how class vs. instance variables are stored and accessed. Here's the code I think should work, but instead produces 4 for every id : class foo(): num = 3 # trying 3 instead of 0 or 1 to make sure the add is working def __init__(self): self.num += 1 self.id = self.num f = foo() g = foo() print f.id # 4

Why do I get this many iterations when adding to and removing from a set while iterating over it?

拥有回忆 提交于 2020-05-10 03:37:05
问题 Trying to understand the Python for-loop, I thought this would give the result {1} for one iteration, or just get stuck in an infinite loop, depending on if it does the iteration like in C or other languages. But actually it did neither. >>> s = {0} >>> for i in s: ... s.add(i + 1) ... s.remove(i) ... >>> print(s) {16} Why does it do 16 iterations? Where does the result {16} come from? This was using Python 3.8.2. On pypy it makes the expected result {1} . 回答1: Python makes no promises about