If the function is inside a class (a method), write it like this:
def get_list_stores(self, code):
And you have to call it over an instance of the class:
ls = LeclercScraper()
ls.get_list_stores(92)
If it's outside a class, write it without the self parameter:
def get_list_stores(code):
Now it can be called as a normal function (notice that we're not calling the function over an instance, and it's no longer a method):
get_list_stores(92)
Answer from Óscar López on Stack OverflowIf the function is inside a class (a method), write it like this:
def get_list_stores(self, code):
And you have to call it over an instance of the class:
ls = LeclercScraper()
ls.get_list_stores(92)
If it's outside a class, write it without the self parameter:
def get_list_stores(code):
Now it can be called as a normal function (notice that we're not calling the function over an instance, and it's no longer a method):
get_list_stores(92)
You don't use "self" arbitrarily - self is recommended to be the first parameter to functions which are written to be methods in classes. In that case, when it is invoked as a method, like in
class A(object):
def get_list_stores(self, code):
...
a = A()
a.get_listscores(92)
Python will insert the "self" parameter automatically on the call (and it will be the object named "a" in the outer scope)
Outside of class definitions, having a first parameter named "self" does not make much sense - although, as it is not a keyword it is not an error per se.
In your case, most likely,t he function you are trying to call is defined in class: you have to call it as an attribute of an instance of the class, and then you simply omit the first parameter - just like in the example above.
been doing leetcode and did a pivotindex questions:
how do I call the function that I have created? What are you suppose to enter for "self"?
pivotIndex(self, nums)
I understand nums is the list that the function is going to go through, but what am I suppose to enter for self?
nums = [1,7,3,6,5,6]
class solution:
def pivotIndex(self, nums):
leftsum = 0
rightSum = sum(nums)
for idx, ele in enumerate(nums):
rightSum -= ele
if leftsum == rightSum:
return idx
leftsum += ele
return -1python - Call a function in it self? - Stack Overflow
python - How to call method with "self" parameter? - Stack Overflow
python - Use of self when calling functions within a class - Stack Overflow
python - How to call a function within a class with self - Stack Overflow
You need to make an instance of the class:
v = VKOAuth2Adapter( whatever parameters it needs, if any )
and then you'll call:
v.complete_login(request, app, token)
where v will automatically become the method's self.
The self parameter is usually not passed explicitly when calling a Python method--it is implicit. So you'd do this:
vk = VKOAuth2Adapter()
vk.complete_login(req, app, token)
The vk instance will be passed as self. You could write the code this way too:
vk = VKOAuth2Adapter()
VKOAuth2Adapter.complete_login(vk, req, app, token)
But that would not be considered normal practice in Python, so use the first option.
Note that if you are calling the method from inside another method in the same class, you'd do it this way:
self.complete_login(req, app, token)
That's because self would be an instance of the class, just as vk was in my previous examples.
You cannot. The function is part of the class, so to access it you must access the class first.
class myClass:
def foo(self):
# do something
myClass().foo()
If you just don't want to type myClass().foo() but only foo then you could just return the myClass() in a new function called foo():
def foo():
return myClass().foo()
You can use self in a class when calling from inside the class itself:
class myClass:
def __init__(self):
pass
def foo(self):
pass
def method2(self):
self.foo()
This does not create a new instance of myClass like calling myClass().foo() would, and you do not need to pass the same arguments that you may have passed in myClass.
Since foo() has a self parameter, that means it is an instance method, and therefore it is only callable from an instance of myClass.
If you want foo to be callable without an instance, use the @staticmethod decorator, and remove self from the parameter list.
Wow, this is confusingly written. Working backward from the code itself:
c = A()
Creates an instance of A. Looking at A:
def aaa(self):
print 'aaa was called'
commands = [aaa]
This is a bit confusingly written; it makes more sense like this:
def aaa(self):
print 'aaa was called'
commands = [aaa]
Defines a method aaa, then a class variable commands which contains aaa as an element. Now, looking at the next line of the program:
c.bbb()
Since A has no bbb and A inherits from B, we consult B:
class B:
def bbb(self):
method = self.commands[0]
method(self)
Since we've established that commands is [aaa], the first line means method = aaa. So the second line is effectively aaa(self).
This line:
method(self) #I can't umderstand this line
Calls the function aaa(). In your function declaration:
def aaa(self):
aaa does takes an argument (self). That's why you have to call it with method(self).
Since self.commands[0] is a function, calling method(self) is equal to:
aaa(self)
Comment if you have something else to ask!