cpython

Different behavior in python script and python idle?

不羁的心 提交于 2019-12-21 03:42:37
问题 In the python idle: >>> a=1.1 >>> b=1.1 >>> a is b False But when I put the code in a script and run it, I will get a different result: $cat t.py a=1.1 b=1.1 print a is b $python t.py True Why did this happen? I know that is compares the id of two objects, so why the ids of two objects are same/unique in python script/idle? I also found that, if I use a small int, for example 1 , instead of 1.1 , the result will be the same in both the python script and python idle. Why did small int and

Python: where is the code for os.mkdir?

我只是一个虾纸丫 提交于 2019-12-20 04:38:35
问题 I've been looking through the code of the os module (just to be clear, I'm looking at the file /usr/lib/python2.7/os.py), and I've been trying to find the code for the mkdir function. From what I could tell, it comes from the 'posix' module, and its a built-in function, same as range or max: >>> import posix >>> posix.mkdir <built-in function mkdir> >>> max <built-in function max> I'm guessing the code for these is written in C somewhere, and the python interpreter knows where to find them.

Finding the source code of methods implemented in C?

旧时模样 提交于 2019-12-18 18:08:11
问题 Please note I am asking this question for informational purposes only I know the title sound like a duplicate of Finding the source code for built-in Python functions?. But let me explain. Say for example, I want to find the source code of most_common method of collections.Counter class. Since the Counter class is implemented in python I could use the inspect module get it's source code. ie, >>> import inspect >>> import collections >>> print(inspect.getsource(collections.Counter.most_common)

How to run a Python project using __pycache__ folder?

北慕城南 提交于 2019-12-18 17:55:44
问题 I want to run a Pythonic project using Python compilation ( .pyc or __pycache__ ). In order to do that in Python2 , I haven't any problem. Here is a simplified example in a Python2 project: Project tree: test2 ├── main.py └── subfolder ├── __init__.py └── sub.py Compile: python -m compileall test2 Project tree after the compile: test2 ├── main.py ├── main.pyc └── subfolder ├── __init__.py ├── __init__.pyc ├── sub.py └── sub.pyc As you can see, several .pyc manually generated. Now I can run

Why does my Sieve of Eratosthenes work faster with integers than with booleans?

♀尐吖头ヾ 提交于 2019-12-18 14:49:34
问题 I wrote a simple Sieve of Eratosthenes, which uses a list of ones and turns them into zeros if not prime, like so: def eSieve(n): #Where m is fixed-length list of all integers up to n '''Creates a list of primes less than or equal to n''' m = [1]*(n+1) for i in xrange(2,int((n)**0.5)+1): if m[i]: for j in xrange(i*i,n+1,i): m[j]=0 return [i for i in xrange(2,n) if m[i]] I tested the speed it ran with %timeit and got: #n: t #10**1: 7 μs #10**2: 26.6 μs #10**3: 234 μs #10**4: 2.46 ms #10**5: 26

Obfuscating python bytecode through interpreter mutation

强颜欢笑 提交于 2019-12-18 11:59:49
问题 Actually, Dropbox made it very well, they were able to secure their desktop application made in python; I researched this a lot, but no good solution better than obfuscation, which is not very secure way to go, and you will end up seeing your code uploaded somewhere. I listened to a session made by Giovanni Bajo (the PyInstaller founder), he said Dropbox does this: Bytecode-scrambling by recompiling your CPython's interpreter, and by this, standard CPython interpreter will not be able to run

Why is string comparison so fast in python?

雨燕双飞 提交于 2019-12-18 11:44:16
问题 I became curious to understand the internals of how string comparison works in python when I was solving the following example algorithm problem: Given two strings, return the length of the longest common prefix Solution 1: charByChar My intuition told me that the optimal solution would be to start with one cursor at the beginning of both words and iterate forward until the prefixes no longer match. Something like def charByChar(smaller, bigger): assert len(smaller) <= len(bigger) for p in

Should importlib.reload restore a deleted attribute in Python 3.6?

情到浓时终转凉″ 提交于 2019-12-18 08:46:34
问题 I'm looking into these two related questions: here and here. I am seeing a behavior I do not expect in Python 3.6, which differs from behavior using plain reload in Python 2.7 (and 3.4). Namely, it seems that a module attribute that would be populated during module initialization or when re-exec-ing the module during a reload, is not restored after its local name is removed with del ... see below: For Python 3.6: In [1]: import importlib In [2]: import math In [3]: del math.cos In [4]: math

OrderedDict comprehensions

心不动则不痛 提交于 2019-12-17 18:32:16
问题 Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict ? Just rebinding the dict name obviously doesn't work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from collections import OrderedDict >>> olddict, dict = dict, OrderedDict >>> {i: i*i for i in range(3)}.__class__ <type 'dict'> So, if it's possible how would I go about doing

Why does str.split not take keyword arguments?

纵然是瞬间 提交于 2019-12-17 16:18:58
问题 I came across this - in my view - strange behaviour: "a b c".split(maxsplit=1) TypeError: split() takes no keyword arguments Why does str.split() not take keyword arguments, even though it would make sense? I found this behavior both in Python2 and Python3. 回答1: See this bug and its superseder. str.split() is a native function in CPython, and as such exhibits the behavior described here: CPython implementation detail: An implementation may provide built-in functions whose positional