python-internals

How does python assign values after assignment operator [duplicate]

限于喜欢 提交于 2019-12-31 22:26:42
问题 This question already has answers here : How do I pass a variable by reference? (26 answers) Closed 4 years ago . Okay a very silly question I'm sure. But how does python assign value to variables? Say there is a variable a and is assigned the value a=2 . So python assigns a memory location to the variable and a now points to the memory location that contains the value 2 . Now, if I assign a variable b=a the variable b also points to the same location as variable a . Now. If I assign a

Where is the “from __future__ import braces” code?

橙三吉。 提交于 2019-12-31 10:07:20
问题 I was wondering what is exactly the code that executed on the command: >>> from __future__ import braces SyntaxError: not a chance so, since python is open-sourced I opened C:\Python27\Lib\__future__.py and looked. surprisingly, I found nothing there that handle importing braces module. so, my question is, where is the code that handle this? what happen when I run that command? 回答1: The code is in future.c: future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) ... else

can a python function call a global function with the same name?

萝らか妹 提交于 2019-12-30 09:53:42
问题 Can I call a global function from a function that has the same name? For example: def sorted(services): return {sorted}(services, key=lambda s: s.sortkey()) By {sorted} I mean the global sorted function. Is there a way to do this? I then want to call my function with the module name: service.sorted(services) I want to use the same name, because it does the same thing as the global function, except that it adds a default argument. 回答1: Python's name-resolution scheme which sometimes is

Why `float` function is slower than multiplying by 1.0?

孤者浪人 提交于 2019-12-30 03:43:10
问题 I understand that this could be argued as a non-issue, but I write software for HPC environments, so this 3.5x speed increase actually makes a difference. In [1]: %timeit 10 / float(98765) 1000000 loops, best of 3: 313 ns per loop In [2]: %timeit 10 / (98765 * 1.0) 10000000 loops, best of 3: 80.6 ns per loop I used dis to have a look at the code, and I assume float() will be slower as it requires a function call (unfortunately I couldn't dis.dis(float) to see what it's actually doing). I

Python 2 assumes different source code encodings

不想你离开。 提交于 2019-12-29 09:29:06
问题 I noticed that without source code encoding declaration, the Python 2 interpreter assumes the source code is encoded in ASCII with scripts and standard input : $ python test.py # where test.py holds the line: print u'é' File "test.py", line 1 SyntaxError: Non-ASCII character '\xc3' in file test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details $ echo "print u'é'" | python File "/dev/fd/63", line 1 SyntaxError: Non-ASCII character '\xc3' in file /dev

python 3.5 type hints: can i check if function arguments match type hints?

筅森魡賤 提交于 2019-12-29 06:22:26
问题 does python 3.5 provide functions that allow to test whether a given argument would fit the type hints given in the function declaration? if i have e.g. this function: def f(name: List[str]): pass is there a python method that can check whether name = ['a', 'b'] name = [0, 1] name = [] name = None ... fit the type hints? i know that 'no type checking happens at runtime' but can i still check the validity of these arguments by hand in python? or if python does not provide that functionality

Accessing the name that an object being created is assigned to

寵の児 提交于 2019-12-29 05:30:52
问题 I'm writing some code to determine the name that an object is assigned to. This is for general debugging work and to further familiarize myself with python internals. I have it structured as a class decorator so that all instances of that class will have their names recorded if it is possible to do. The code is fairly long so I won't post it unless asked. The general technique is as follows though decorate the class' __init__ method with the code to do what I want set caller = inspect

How print statement create a local variables

给你一囗甜甜゛ 提交于 2019-12-29 04:15:30
问题 Question are at the end of this post. First snippet: empty local variable dictionary. def outer(): x = 1 def inner(): print "Local variables: %s" % locals() return inner() print outer() Output: Local variables: {} Second snippet: print inside inner() function and creating local variable entry. def outer(): x = 1 def inner(): print x print "Local variables: %s" % locals() return inner() print outer() Output: 1 Local variables: {'x': 1} Third Snippet : del x from inside the inner function: def

How exactly is Python Bytecode Run in CPython?

偶尔善良 提交于 2019-12-29 02:34:44
问题 I am trying to understand how Python works (because I use it all the time!). To my understanding, when you run something like python script.py, the script is converted to bytecode and then the interpreter/VM/CPython–really just a C Program–reads in the python bytecode and executes the program accordingly. How is this bytecode read in? Is it similar to how a text file is read in C? I am unsure how the Python code is converted to machine code. Is it the case that the Python interpreter (the

Is it possible to “hack” Python's print function?

a 夏天 提交于 2019-12-28 03:19:05
问题 Note: This question is for informational purposes only. I am interested to see how deep into Python's internals it is possible to go with this. Not very long ago, a discussion began inside a certain question regarding whether the strings passed to print statements could be modified after/during the call to print has been made. For example, consider the function: def print_something(): print('This cat was scared.') Now, when print is run, then the output to the terminal should display: This