What's the Difference Between _Builtin_ and _Builtins_

What's the difference between __builtin__ and __builtins__?

Straight from the python documentation:
http://docs.python.org/reference/executionmodel.html

By default, when in the __main__ module, __builtins__ is the
built-in module __builtin__ (note: no 's'); when in any other
module, __builtins__ is an alias for the dictionary of the
__builtin__ module itself.

__builtins__ can be set to a user-created dictionary to create a
weak form of restricted execution.

CPython implementation detail: Users should not touch __builtins__; it is strictly an implementation detail. Users
wanting to override values in the builtins namespace should import
the __builtin__ (no 's') module and modify its attributes
appropriately. The namespace for a module is automatically created the
first time a module is imported.

Note that in Python3, the module __builtin__ has been renamed to builtins to avoid some of this confusion.

Python3: What is the difference between keywords and builtins?

Keywords are core language constructs handled by the parser. These words are reserved and cannot be used as identifiers: http://docs.python.org/reference/lexical_analysis.html#keywords

Builtins are a list of commonly used, preloaded functions, constants, types, and exceptions: http://docs.python.org/library/functions.html

In Python 3, the overlapping words, False, None, and True are builtin constants that are protected from assignment by the parser. This prevents accidental overwriting with something like True=10. As a keyword, this assignment can be blocked:

>>> True = 10
SyntaxError: assignment to keyword

The remaining builtins are not protected and could be reassigned with something like __builtins__.list = mylist.

Differentiating between built-in functions vs built-in methods in Python

A method is a function which is applicable to a certain class, while a function can be used in any valid class. Like the sort method for the list class sorts the list. Methods of mutable types mostly change the item, so list.sort would set the value of list to the sorted value of list and return None. But methods of immutable types like strings will return a new instance of the thing as seen below.

question = "How is this?"
question.replace("How", "What") # Returns "What is this", but does not change question.
print(question) # Prints "How is this?"
print(question.replace("How", "What")) # Prints "What is this"

Built in functions like sorted do not change the item, they return a new version, or instance, of it.

list1 = [4,3,6,2]
sorted(list1) # Returns [2,3,4,6], but does not modify list.
print(list1) # Prints [4,3,6,2]
list1.sort() # Returns None, but changes list.
print(list1) # Prints [2,3,4,6]

When you use a method, you put a period after the variable to show that it can only be used for that specific class. Why some functions require arguments while some methods don't - like sorted(list) requires list, but list.sort() doesn't require arguments, is that when you use a method on a class, Python by default passes in a parameter called self, which is the actual variable, list in this case. If you have worked with JavaScript, self is something like the this keyword in JS.

So when you enter list.sort(), Python is actually running the function sort inside the list class passing it a parameter of self.

What is the relationship between Python built-in modules and built-in types/functions?

There's no relationship whatsoever between the built-ins namespace and built-in modules. Stuff that's accessible without importing is that way because it's been inserted into a special built-ins namespace, available through the builtins module, while built-in modules are a special subset of standard library modules that are compiled directly into the Python interpreter executable. They're completely unconnected usages of the word "built-in".

The "built-in" in <built-in function whatever> is another completely unrelated meaning - when you talk about built-in types and functions being usable without importing, that's really a property of the built-ins namespace, while a function showing up as <built-in function whatever> just means it's written in C.

why __builtins__ is both module and dict

I think you want the __builtin__ module (note the singular).

See the docs:

27.3. __builtin__ — Built-in objects

CPython implementation detail: Most modules have the name __builtins__ (note the 's') made available as part of their globals. The value of __builtins__ is normally either this module or the value of this modules’s [sic] __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.

python: builtin function redefinition, with different arguments

All from __future__ import print_function does is toggle a flag in the parser to make it stop treating print as a keyword; once that's done, references to print seamlessly become like references to any other non-keyword that constitutes a legal variable name (they go through LEGB lookup, and get found in the B of LEGB, the builtins scope). This behavior is hard-coded deep in the Python interpreter; there is no way to achieve a similar effect for any other keyword without building a custom version of Python, or engaging in some other egregious hackery well beyond the scope of any reasonable problem.

As of 2.6, __builtin__ has a print function on it, so any module that uses from __future__ import print_function (and can therefore reference the name print as opposed to the keyword print) will see __builtin__.print (if it hasn't been shadowed by something in the local, nested or global scope). It's still there for every module, but in modules without the __future__ import, references to print are resolved to the keyword at compile time and replaced with the raw bytecode implementing the special print statement (the same way del and return behave; you can't name a variable either of those things for the same reason, they're keywords), so modules without the import never get a chance to lookup the print function.

This doesn't generalize to other cases, because __future__ has no other cases where one of its features converts a keyword to a non-keyword. For all other actual built-in functions, being able to overwrite them on a per-module basis is as simple as assigning to the name at global scope (shadowing it for that module), e.g.:

def abs(x):
return x # Who needs absolute value anyway?
# From here on out, references to abs in this module see your override, not the built-in

While it's possible to reassign the built-in globally, it's a terrible idea (since every other module that uses it likely relies on the built-ins original behavior). That said, it's not hard:

import __builtin__

def abs(x):
return x # Who needs absolute value anyway?

__builtin__.abs = abs
# *Every* module now sees your terrible monkeypatch, on your own head be it


Related Topics



Leave a reply



Submit