I am creating a method object from string using "exec()". when I do dir(), I am able to see that the methods have been created in the current module. I need the method objects directly from exec() since there are lot of methods with same names which are currently getting replaced.
Related
The Python official document specifies that a docstring is a string literal that occurs at the beginning of a function. And it can be accessed using the __doc__ attribute.
If I have a function that will be called many many times, does that mean the docstring will be declared every time the function is called?
If this is the case, would it be more efficient to design docstring in such a way that it is stored in __doc__ but not being declared every time the function is called?
every time you start a python program, they are "important" into memory only "once", parsed so that every "object" properties are determined and all objects are put into their separate memory locations and then linked together in the memory to make it a whole running system (remember the object nature of python).
second behavior is when you don't restrict the python interpreter. If you import your files, then, in addition to the above steps, it writes these objects into more durable .pyc files under __pycache__ folder at the same level of the file.
In this process, new objects are created to have a __doc__ property object when certain keywords are parsed, mainly class and def. These __doc__ of each are then either kept empty, filled some default by inheritance, or if it has a docstring then it is written inside.
You can see this behavior on different objects, created with/out supplying a docstring, simply by using dir(objectname). To answer your question, you can use this command throughout your program.
However, this is true only for static written code. If you are trying to make objects on the fly, especially within loops, then your objects will be actively created and destroyed, thus there will be almost no optimization against them and docstrings will be created again and again.
consider these two:
def staticMethod():
pass
for i in range(5):
def activeMethod():
pass
print(staticMethod,"s")
print(activeMethod,"a")
while staticMethod is served from the same memory location, the memory address for activeMethod changes. you will see an altering between few values because python can still optimize since this one is a simple example.
So keep yourself aware of this distinct behavior, especially of loops.
I am using the Python 3 C API to interface with some Python code.
I have instantiated a Python object from C++ using PyObject_Call and I have been able to modify some of its attributes using PyObject_SetAttrString. One the attributes I would like to modify is a callback function, how can I instantiate a Python object from one of my C++ functions?
What you need to do is to create a Python object which is callable, and when it is called, it ends up calling a function implemented in C (or C++).
I would implement a new class, as specified in Python Type Objects docs. It may implement any kind of constructor etc. you may need, but most importantly for this purpose, it should implement the tp_call, which will make instances of this class callable (as if there was a __call__ method implemented in Python).
The implementation of tp_call is in C/C++ and you can create instances of this class, which are Python objects and can be passed to any Python code.
See also: Defining Extension Types: Tutorial
I have a python class which uses class variables to maintain some instance-wide state. However, when attempting to test various scenarios via pytest I'm realizing that the class-level attributes are being shared across the tests since it's the same interpreter being used. Is there any pattern or way around this outside of running each class as a separate invocation of the pytest command or resetting every class attribute manually between tests? It's looking like a big no but wanted to ask and make sure.
p.s. For those who will immediately ask why I'm using class attributes. I'm using class attributes for state because this code will run on AWS Lambda. With this pattern I can cache objects in memory as class-attributes between lambda invocations and be assured that instance attributes are cleared each time the lambda runs. It's an attempt at using some OOP and writing base lambda classes with logging and various helpers already implemented that other devs can leverage
https://docs.python.org/3.3/library/imp.html#imp.reload
When reload(module) is executed:
Python modules’ code is recompiled and the module-level code
reexecuted, defining a new set of objects which are bound to names in
the module’s dictionary. The init function of extension modules is not
called a second time. As with all other objects in Python the old
objects are only reclaimed after their reference counts drop to zero.
The names in the module namespace are updated to point to any new or
changed objects. Other references to the old objects (such as names
external to the module) are not rebound to refer to the new objects
and must be updated in each namespace where they occur if that is
desired.
This question already has answers here:
Python (and Python C API): __new__ versus __init__ [duplicate]
(6 answers)
Closed 2 years ago.
I recently started following Python after studying Java. I'm confused with the way of python interpreter's object construction.
Compared to Java when we construct an object when simply provide our arguments, for Python that is the case too.
But I can't think why the __init()__ method requires a self parameter when we define it in our class.
I read this question and I got that the methods require a self parameter because python calls a method in the format ClassA.methodA(ObjectA, arg1, arg2).
But I really don't get why the __init()__ method require this.
Is it because the way that Python generate an object differs from the way that Java generates an object.
I really appreciate if someone can explain it to me.
Why must ‘self’ be used explicitly in method definitions and calls?
The idea was borrowed from Modula-3. It turns out to be very useful, for a variety of reasons.
First, it’s more obvious that you are using a method or instance attribute instead of a local variable. Reading self.x or self.meth() makes it absolutely clear that an instance variable or method is used even if you don’t know the class definition by heart. In C++, you can sort of tell by the lack of a local variable declaration (assuming globals are rare or easily recognizable) – but in Python, there are no local variable declarations, so you’d have to look up the class definition to be sure. Some C++ and Java coding standards call for instance attributes to have an m_ prefix, so this explicitness is still useful in those languages, too.
Second, it means that no special syntax is necessary if you want to explicitly reference or call the method from a particular class. In C++, if you want to use a method from a base class which is overridden in a derived class, you have to use the :: operator – in Python you can write baseclass.methodname(self, ). This is particularly useful for __init__() methods, and in general in cases where a derived class method wants to extend the base class method of the same name and thus has to call the base class method somehow.
Finally, for instance variables it solves a syntactic problem with assignment: since local variables in Python are (by definition!) those variables to which a value is assigned in a function body (and that aren’t explicitly declared global), there has to be some way to tell the interpreter that an assignment was meant to assign to an instance variable instead of to a local variable, and it should preferably be syntactic (for efficiency reasons). C++ does this through declarations, but Python doesn’t have declarations and it would be a pity having to introduce them just for this purpose. Using the explicit self.var solves this nicely. Similarly, for using instance variables, having to write self.var means that references to unqualified names inside a method don’t have to search the instance’s directories. To put it another way, local variables and instance variables live in two different namespaces, and you need to tell Python which namespace to use.
reference: https://docs.python.org/3/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
From the c-api, I would like to call a python function by name. I would then be calling the function with a list of python objects as arguments.
It is not clear to me in the Python documentation how I would get a "Callable" object from the main python interpreter.
Any help appreciated in:
Getting the address from the function
Calling the function with my PythonObject's as arguments.
I'm using Python 2.x series for my development.
Basically, you use the Python C API to get the module the function is contained in, then query the module dictionary for the function. That's more or less the same what the Python runtime does internally when your Python code invokes a function from somewhere.
Relevant functions from the API to look at are PyImport_ImportModule, PyModule_GetDict, PyDict_GetItem and the PyObject_CallXXX family of functions.