How to retrieve/know function's default values [duplicate] - python

This question already has answers here:
Get a function argument's default value?
(7 answers)
How to find out the default values of a particular function's argument in another function in Python?
(4 answers)
How can I read a function's signature including default argument values?
(9 answers)
Closed 5 years ago.
I am a python beginner and I currently struggle with some (probably quite simple) stuff. I would like to know the default parameters of a python function, more specifically of cv2.ORB_create().
With the following code I managed to see the 'documentation string':
import cv2
orb = cv2.ORB_create()
print(cv2.ORB_create.__doc__)
However, print(cv2.ORB_create.__defaults__) just gives me an error;
AttributeError: 'builtin_function_or_method' object has no attribute '__defaults__'
Maybe I am missing a link between functions, modules, etc. but I am really stuck...
Since the concern was brought up that this is a duplicate. I also tried inspect.signature and by extension inspect.getargspec, but this give me another error ValueError: no signature found for builtin <built-in function ORB_create>.

cv2.ORB_create() seems to be a function written with the Python extensions (In C instead of Python). As such, it isn't a "normal" function object, and there is no way to see the default values reliably (As it is handled manually in C).
One possible solution would be to look at the __text_signature__ attribute, but this may not be reliable. See What are __signature__ and __text_signature__ used for in Python 3.4.

Related

Understanding how Python builtins are mapped to underlying C implementation [duplicate]

This question already has answers here:
PyCharm, what is python_stubs?
(2 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
If I go to the implementation of let's say the next() builtin function, I'm forwarded to the builtins.py module to following code:
def next(iterator, default=None): # real signature unknown; restored from __doc__
"""
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator
is exhausted, it is returned instead of raising StopIteration.
"""
pass
Now, it looks like this functions does nothing but obviously that's not the case.
I understand that this function is implemented in C under the hood, but how and when is this function(or other builtin functions) mapped to the underlying C implementation?
If you have an answer to this question, can you please also provide links that I can read in order to better unterstand this topic?
I'm not asking, where the code is, but how and when the function is mapped to that code
Thank you.

Obtaining closures at runtime [duplicate]

This question already has answers here:
Given a function with closure, can I refer back to it's closure scope?
(1 answer)
What exactly is contained within a obj.__closure__?
(4 answers)
Closed 2 years ago.
I would like to know if there is any method to check whether two functions have the same arguments at runtime in python 3.
Basically, I have this function (func) that takes another function and an argument. I want to check the values assigned to args in the lambda function
func(another_func, args):
return lambda(x : another_func(x, args))
It is not feasible to run the code before and check the results because I am implementing a lazy framework. My main goal is to be able to understand what are the arguments of the function because there is one variable argument that I do not care but there is one static that is created before running the function.
##EDIT
I actually solved this problem using the inspect module (getclosure) for those who are interested!
I actually solved this problem using the inspect module (getclosure) for those who are interested!
Extension (Martijn Pieters):
I think you are referring to getclosurevars(). You can also just access function.closure, and access the value of each cell via its cell_contents attribute.

how to use print both as a function and a variable in one program [duplicate]

This question already has answers here:
Why does code like `str = str(...)` cause a TypeError, but only the second time?
(20 answers)
Closed 6 years ago.
I have used print to store value like:
print=3
After then I am not able to use it to print any message:
print('a message')
Its giving error:
'int object is not callable'
Is there any way to use print both as a variable and a functions? If not then Why not python just makes built-in function names as keyword to remove this conflict?
Functions and data share the same namespace in Python -- as they do in many other languages (the entire family of LISP-1s comes to mind first, including Scheme and Clojure; also Ruby, Groovy, and I'm sure many more).
Thus no, you cannot do this. Widely available checkers (pylint, pychecker, etc) will catch and report on attempts to shadow builtins (such as print) with data.

How methods work in python? [duplicate]

This question already has an answer here:
python string module vs str methods
(1 answer)
Closed 6 years ago.
I am new to programming therefore may sound idiotic. I am learning python where I am not able to understand how few methods like upper(), split() etc work.
I mean you directly use like below:
"ABC".upper() or "abc,xyz".split(",")
Or, you can first import string and then call these methods like below:
import string
string.upper("abc")
string.split("abc,xyz", ",")
What is the difference, and how would we import string module when we can achieve the same output without importing it.
Are there similar cases exist apart from string module?
In fact, one of the paradigm you can use in Python is the Object Oriented Programming, where you modify object state through "methods" like this: myobject.mymethod().
Syntactically, it means that the first argument of the method mymethod() is in fact the object itself. But, as Python want also to deal with other paradigms (functional programming, imperative programming, and so on), there is two syntactical ways to address this method.
One is simply as I mentioned before: myobject.mymethod(), and the other one is simply to consider that the first argument is the object itself: mymethod(myobject).
More precisely, you can realize that when you define by yourself a method because you have to specify the first argument by self which is a reference to the object itself like this:
def mymethod(self):
pass

Why does Python require the "self" parameter? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
python ‘self’ explained
Why do you need explicitly have the “self” argument into a Python method?
Why does Python require the "self" parameter for methods?
For example def method_abc(self, arg1)
And is there ever a date that the need for it will be removed?
Python gives you the option of naming it something other than self, even though the standard is to name it self. Just as it gives you the option of using tabs for indents, even though the standard is to use spaces.
In other words, it's not just "assumed" because...
To give you naming flexibility
To make it clearer that something will be passed self (or not).

Categories

Resources