cpython

CPython's static object address and fragmentation

拈花ヽ惹草 提交于 2019-12-10 23:30:41
问题 I read For CPython, id(x) is the memory address where x is stored. And it's a given the id of an object never changes, which means an object always stored at a given memory address in its lifetime. This leads to the question: what about fragmentation of (virtual) memory? Say an object A is at address 1 (has id 1), takes up 10 bytes, so it takes up addresses 1-10. Object B has id 11 and takes up bytes 11-12, and object C takes up addresses 13-22. Once B goes out of scope and gets GC'd, we'll

Python: why partition(sep) is faster than split(sep, maxsplit=1)

爱⌒轻易说出口 提交于 2019-12-10 17:52:43
问题 I found an interesting thing that partition is faster than split when get whole substring after the separator. I have tested in Python 3.5 and 3.6 (Cpython) In [1]: s = 'validate_field_name' In [2]: s.partition('_')[-1] Out[2]: 'field_name' In [3]: s.split('_', maxsplit=1)[-1] Out[3]: 'field_name' In [4]: %timeit s.partition('_')[-1] 220 ns ± 1.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each) In [5]: %timeit s.split('_', maxsplit=1)[-1] 745 ns ± 48.8 ns per loop (mean ± std.

Is IronPython usable as a replacement for CPython?

前提是你 提交于 2019-12-10 17:18:49
问题 Has IronPython gotten to a point where you can just drop it in as a replacement for CPython? To clarify: I mean can IronPython run applications originally written for CPython (no .NET involved, of course) 回答1: Yes, pretty much, at least on Windows with "real" (Microsoft) .NET underneath. If you're depending on C-coded extensions, chances are that ironclad can bail you out; you get 2.6 support, just about every CPython standard library or third-party extension module (maybe not trivial for

Python: equality for Nan in a list?

核能气质少年 提交于 2019-12-10 17:15:05
问题 I just want to figure out the logic behind these results: >>>nan = float('nan') >>>nan == nan False # I understand that this is because the __eq__ method is defined this way >>>nan in [nan] True # This is because the __contains__ method for list is defined to compare the identity first then the content? But in both cases I think behind the scene the function PyObject_RichCompareBool is called right? Why there is a difference? Shouldn't they have the same behaviour? 回答1: But in both cases I

Value of Py_None

£可爱£侵袭症+ 提交于 2019-12-10 14:53:26
问题 It is clear to me that None is used to signify the lack of a value. But since everything must have an underlying value during implementation, I'm looking to see what value has been used in order to signify the absence of a value, regarding CPython . I understand, based on the documentation, that NoneObject is a singleton. Since my c skills are rusty, my best, amateur guess, would be that the value of None would be the pointer to the memory allocated for the Py_None object; since it is a

When does CPython garbage collect?

微笑、不失礼 提交于 2019-12-10 10:41:14
问题 If my understanding is correct, in CPython objects will be deleted as soon as their reference count reaches zero. If you have reference cycles that become unreachable that logic will not work, but on occasion the interpreter will try to find them and delete them (and you can do this manually by calling gc.collect() ). My question is, when do these interpreter-triggered cycle collection steps happen? What kind of events trigger them? I am more interested in the CPython case, but would love to

Extracting SWIG wrapped C++ instance/pointer for use in Cython

折月煮酒 提交于 2019-12-09 22:05:41
问题 I have an instance of a class from SWIG-wrapped C++ library from which I would like to extract its reference, in order to be able to use it inside a Cython file, in which I am directly linking to the same C++ library by using a more lightweight self-made Cython wrapper of the same class. I know it would not be as easy as accessing some hidden attribute, but I imagine there might be some function within SWIG or CPython that could potentially do that if linked to from within Cython (some

反转Python中的字符串

自作多情 提交于 2019-12-09 20:01:06
Python的 str 对象没有内置的 reverse 函数。 实施此方法的最佳方法是什么? 如果提供非常简洁的答案,请详细说明其效率。 例如,是否将 str 对象转换为其他对象等。 #1楼 在python中反转字符串,而不使用reversed()或[::-1] def reverse(test): n = len(test) x="" for i in range(n-1,-1,-1): x += test[i] return x #2楼 为字符串实现反向函数的最佳方法是什么? 我对这个问题的经验是学术上的。 但是,如果您是专业人士,正在寻找快速解答,请使用以 -1 步长的切片: >>> 'a string'[::-1] 'gnirts a' 或可读性更好(但由于方法名称查找和加入给定迭代器时join形成列表的事实而变慢), str.join : >>> ''.join(reversed('a string')) 'gnirts a' 或为了可读性和可重用性,将切片放入函数中 def reversed_string(a_string): return a_string[::-1] 接着: >>> reversed_string('a_string') 'gnirts_a' 更长的解释 如果您对学术博览会感兴趣,请继续阅读。 Python的str对象中没有内置的反向函数。

Python string concatenation internal details

廉价感情. 提交于 2019-12-09 13:01:58
问题 Assume that we have a list of strings and we want to create a string by concatenating all element in this list. Something like this: def foo(str_lst): result = '' for element in str_lst: result += element return result Since strings are immutable objects, I expect that python creates a new str object and copy contents of result and element at each iteration. It makes O(M * N^2) time complexity, M is the length of each element and N is the size of the list. However, my experiment shows that it

What are the rules for cpython's string interning?

北城余情 提交于 2019-12-09 00:34:18
问题 In python 3.5, is it possible to predict when we will get an interned string or when we will get a copy? After reading a few Stack Overflow answers on this issue I've found this one the most helpful but still not comprehensive. Than I looked at Python docs, but the interning is not guaranteed by default Normally , the names used in Python programs are automatically interned, and the dictionaries used to hold module, class or instance attributes have interned keys. So, my question is about