I wasn't looking what I had previously written on the line so I accidently declared a variable in ipython as:
np.zerosn=10
Surprisingly this was allowed. So I thought that maybe it was because you can name use periods in your variable names, but that is not the case. So I'm wondering what is actually happening. Is this adding a new variable to the numpy module?
Yes.
In general, (most/many) python objects have dynamic attribute spaces, and you can stick whatever you want onto them whenever you want. And modules are just objects. Their attribute space is essentially the same as their global scope.
Pure python functions are another (perhaps surprising) example of something onto which you can stick arbitrary attributes, though these are not associated with the function's local scope.
Most 'builtin' types (i.e. those which are implemented in extension modules, rather than those that are found in the __builtins__ module) and their instances, do not have dynamic attribute spaces. Neither do pure python types with __slots__.
Related
I've been using Python for a good period of time. I have never found out how built-in functions work. In different words, how are they included without having any module imported to use them? What if I want to add to them (locally)?
This may seem naive. But, I haven't really found any answer that explains comprehensively how do we have built-in functions, global variables, etc., available to us when developing a script.
In a nutshell, where do we include the builtins module?
I have encountered this question. But it gives a partial answer to my question.
The not-implementation-details part of the answer is that the builtins module, or __builtin__ in Python 2, provides access to the built-ins namespace. If you want to modify the built-ins (you usually shouldn't), setting attributes on builtins is how you'd go about it.
The implementation details part of the answer is that Python keeps track of built-ins in multiple ways. For example, each frame object keeps track of the built-in namespace it's using, which may be different from other frames' built-in namespaces. You can access this through a frame's f_builtins attribute. When a LOAD_GLOBAL instruction fails to find a name in the frame's globals, it looks in the frame's builtins. There's also a __builtins__ global variable in most global namespaces, but it's not directly used for built-in variable lookup; instead, it's used to initialize f_builtins in certain situations during frame object creation. There's also a builtins reference in the global PyInterpreterState, which is used as default builtins if there's no current frame object.
I'm wondering, if Python offers something similar to the package keyword in Perl. This keyword in effect creates a labeled namespace just anywhere in the code.
As far as I know, similar namespacing in Python is only possible by putting that code into a file and import it. But what if I have the code in a variable (e.g. read from some configuration file of my script)?
So in other words: Is there a way to eval Python code within an arbitrary namespace? In Perl I would just add
package my_pack;
at the beginning of that code and then eval it (within a namespace called my_pack)
Thanks for any help.
No, Perl's and Python's module systems work very differently. It is not possible to explicitly declare a specific Python module.
For a Python eval() or exec() that should execute the code within the context of a particular module, consider which aspects define this module for your purposes – the important aspect is likely that module's global variables. You can provide these explicitly, and capture the current environment via the globals() function. The environment is just a dict, which you can copy if you want to avoid modifications of the module's environment.
I have a library that uses quite a few global variables, that I'd like to use in a multi-threaded application, however what I'd like to know is if I import the library inside a function, will the library's global variables etc. be separate copies, so that they don't corrupt each other?
No. There will only be a single instance of the 'global' variables (presumably defined at the top level of the module).
A module is only ever imported once, importing it a second time simply adds it to the appropriate namespace.
No. Python has Module Scope here whereby the global variables you have defined in that module if mutated by other threads without locking will have unpredictable behaviour.
I would refactor your code into a set of objects with remove the use of globals and possibly also implement locking if you intend to share the same objects.
I have heard a lot about namespace as a feature for programming language. Thus, can not help thinking:
Does Python have so called namespace feature?
Quote from Python documentation:
A namespace is a mapping from names to objects. Most namespaces are currently implemented as Python dictionaries, but that’s normally not noticeable in any way (except for performance), and it may change in the future. Examples of namespaces are: the set of built-in names (containing functions such as abs(), and built-in exception names); the global names in a module; and the local names in a function invocation. In a sense the set of attributes of an object also form a namespace. The important thing to know about namespaces is that there is absolutely no relation between names in different namespaces; for instance, two different modules may both define a function maximize without confusion — users of the modules must prefix it with the module name.
For a good overview, see the "Python Scopes and Namespaces" section of the Python tutorial at https://docs.python.org/2/tutorial/classes.html#python-scopes-and-namespaces.
I tried to look around but I couldn't find anything clear about this topic.
Are built-in functions implemented in a module that is automatically imported every time Python is launched? In the case which is the module?
Or are built-in functions just embedded functions inside the Python interpreter?
For CPython, the built-in functions are (for the most part) implemented in the bltinmodule.c file.
The exceptions are mostly the types; things like str and dict and list have their own C files in the Objects directory of the C source; these are listed as a table in the bltinmodule source.
Technically speaking, this is treated as a separate module object by the implementation, but one that is automatically searched when the current global namespace does not contain a name. So when you use abs() in your code, and there is no abs object in the global namespace, the built-ins module is also searched for that name.
It is also exposed as the __builtin__ module (or builtins in Python 3) so you can access the built-in names even if you shadowed any in your code. Like the sys module, however, it is compiled into the Python binary, and is not available as a separate dynamically loaded file.