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).
Related
This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 2 years ago.
I am new to python, and realized that i can assign print i.e. an inbuilt function as a variable, then when i use print('hello world')this shows the exact error that i faced
I am familiar to c++ and even in that we were never allowed to use an inbuilt function as a variable name.
those were the fundamental rules for naming a variable
If python.org has issued the new version I'm sure they would have done it for a reason, bbut i want to know how do i access my print statement after assigning a value to it?
you won't be able to access your print function unless you do hacky things, which I recommend not to do them in the middle of your code.
Also it is good to know that python (as c++) has scopes for variables, and variables "die" and they are no longer accessible when scope ends. For instance:
def change_print_value():
print = 3
change_print_value()
print('Print works as expected')
It is a good practice to avoid using reserved keywords as variable names. Any IDE has the keywords highlighted, so you can easily realize when you are using a keyword where you shouldn't.
print is not part of the reserved keywords list in python. Here's a comprehensive list of reserved words.
Functions are first class objects in python, so that means they can be treated and manipulated as objects. Since print is a function (and an object), when you call print = 1, you reassign the variable print to have a value of 1, so the functionality "disappears".
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.
This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 6 years ago.
Well, I am a newbie in Python and I am not able to understand the difference in using self and this keywords in Python.
This is the code that uses self as parameter :
class restaurant():
bankrupt = False
def open_branch(self):
if not self.bankrupt:
print("branch open")
x=restaurant()
print(x.bankrupt)
y=restaurant()
y.bankrupt=True
print(y.bankrupt)
And this is the code that uses this as parameter :
class restaurant():
bankrupt = False
def open_branch(this):
if not this.bankrupt:
print("branch open")
x=restaurant()
print(x.bankrupt)
y=restaurant()
y.bankrupt=True
print(y.bankrupt)
Both these approaches gave me the same output. So I m not able to understand why we use self when this solves our problem. Maybe my interpretation of self is wrong. I looked a lot of internet stuff but did not found anything relevant.
Can anyone please solve my issue.
It is mentioned in Python document:
Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.
Using the name self is just a (strong) convention.
You are free to use any name instead, as long as you are constant.
It is highly recommended to use self though.
In general, Python gives you a lot of freedom doing things the way you like.
On the other hand, there are many conventions like how to name you variables (compare PEP8). In 99% of the cases it is best to adhere to this conventions. But if you a 1% case, you can do it differently. I have never seen a case for not using the name self though.
PEP8 recommends the use of self:
Always use self for the first argument to instance methods.
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
python 'self' explained
I am a beginner in Python. I was going through the tutorials on Classes and Iterators when I had a doubt that I was unable to explain to myself. The program text below was a part of a class which calculates area.
def __init__(self,len,wid):
self.length=len
self.width=wid
def calculate_area(self)
return self.length*self.width
def print_area(self)
print 'Area='+str(self.calculate_area())
What I am unable to understand is why do the function's argument list have "self"? What is its role? Why are every variable resolved with "self"?
This is similar to this pointer in C++ (if you have come from C++ background)
Typical usage would be that the members of objects can be referenced by self in case if there is an ambiguity. e.g.
def calculate_area(self, length)
return self.length*self.width
Above length is an argument for calculate_area function.
if the object also has length member then it can be resolved by using self.length
Refer existing answer here:
What is the purpose of self?
I really don't know I'm too new on the Python world but I think that Python does not provide the this value as C# or Java do, so this is the mechanism that Python use to define itself in its classes.
Anyway you can see that you don't need to pass the self as parameter in the function call, because Python does for you.
This is my theory, but I'm also interested to know it so If anyone can say more about this, I think we will be very thankfull.
See you!