I know we can import modules and just embed Python code in C++ and evaluate it. But how can I use built-in functions like print or open? These functions off course aren't module. Evaluating embedded open statement just gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'open' is not defined
Stuck. Please help me.
Try importing the builtins and io module and if you want any other function just call the __module__ attribute to find about which module to import
>>> print.__module__
'builtins'
>>> open.__module__
'io'
Related
I want to add functions to python's built in module. As a example, there is no find function in list. I want to add it. There is some other functions I want to add. Can somebody please tell me how can I do that? Is their any way I can do that?
Adding functions to the builtins module is simple:
import builtins
builtins.find = my_find_function
But the built in classes are usually immutable, so they cannot be changed.
>>> list.hello = "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'
Something I'm doing in PIL is giving me back an object of a class I don't recognize. It may be a fairly thin wrapper around a C data structure or something. How can I get Python to tell me where to look for more information?
Here are some failed attempts to use introspection to learn more:
>>> import os, PIL
>>> obj = PIL.Image.open(os.path.expanduser("~/Desktop/foo.png")).getdata()
>>> type(obj)
<type 'ImagingCore'>
>>> ImagingCore
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ImagingCore' is not defined
>>> PIL.ImagingCore
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ImagingCore'
>>> obj.__class__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __class__
>>> obj.__module__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __module__
>>> import inspect
>>> inspect.getsource(obj)
...
TypeError: <ImagingCore object at 0x105e64b50> is not a module, class, method, function, traceback, frame, or code object
>>> inspect.getsource(type(obj))
...TypeError: <module '__builtin__' (built-in)> is a built-in class
>>>
PIL's core functionality is implemented in module _imaging, written, as you guessed, in the C language -- see _imaging.c (3281 lines...:-) in the top level source directory, Imaging-1.1.7. That code doesn't pay any attention to introspection -- rather, it's 100% focused on performance. I believe it doesn't even bother to expose anything but functions (it does implement several types, including ImagingCore, but doesn't even expose those types' names to Python -- only generates and uses them internally).
So Python won't tell you where to look for more info because the library, in turn, doensn't tell Python:-). As the docs for getdata at http://effbot.org/imagingbook/image.htm says:
Note that the sequence object returned by this method is an internal
PIL data type, which only supports certain sequence operations,
including iteration and basic sequence access. To convert it to an
ordinary sequence (e.g. for printing), use list(im.getdata())
...and "that's all she wrote" -- nothing but that small subset of sequence operations gets exposed.
I am new to Python,
and have started working on code written by others.
In the source of packages downloaded from Pypi I have noticed the use of
import __module__
to use functions and classes defined in the src folder of packages.
Is this common practice? I actually can't really understand this kind of syntax,
could you explain it to me or send me to some reference?
It's a python convention for some builtin objects. From PEP8:
__double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.
But in the end it is not a "kind of syntax" to understand or not, __module__ is just a name with underscores in it. It's different from and completely unrelated to module.
An example to show you it's just a name:
>>> __foo__ = 42
>>> print foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> print _foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_foo' is not defined
>>> print __foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__foo' is not defined
>>> print __foo__
42
>>> type(__foo__)
<type 'int'>
Nothing inherently special about it.
Without more info about where you saw this though, it's hard to say what the author's intention was. Either they are importing some python builtins (e.g. from __future__ import...) or they are ignoring PEP8 and just named some objects in this style because they think it looks cool or more important.
Something's acting up in my math package I think and I want to ensure I'm loading the correct module. How do I check the physical file location of loaded modules in python?
Use the __file__ attribute:
>>> import numpy
>>> numpy.__file__
'/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'
Note that built-in modules written in C and statically linked to the interpreter do not have this attribute:
>>> import math
>>> math.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
An other way to obtain the path to the file is using inspect.getfile. It raises TypeError if the object passed is a built.in module, class or function.
On a side note, you should avoid using names that conflict with language built-ins or standard library modules. So, I'd suggest you to rename your math package to something else, or, if it is part of a package like mypackage.math, to avoid importing it directly and use mypackage.math instead.
Check themodule.__file__.
import urllib
print urllib.__file__
>>> import math
>>> math.__file__
'/usr/lib/python2.7/lib-dynload/math.so'
I've got file named recommend.py. It has a dict data named critics.
When I try to reload it in the interpreter it gives the following error:
>>> from recommend import critics
>>> reload(recommend.py)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'recommend' is not defined
>>>
I'm new to python. Please help me.
recommend.py is parsed as recommend . py which means that python looks for an object bound to the name recommend and then tries to get the py attribute from it. That doesn't work because you don't have an object named recommend in the current namespace and because even if you did have an object bound to that name, it probably wouldn't have an attribute py.
Of course, you'll need to give reload an actual module object. Something more like:
import recommend
reload(recommend)
reload() takes a module object, not a filename:
import recommend
reload(recommend)