如何使用参数中带有关键字“self”的 Python 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18472904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use a Python function with keyword "self" in arguments
提问by imoum
i have a function that retrieve a list of stores in Python this functions is called :
我有一个在 Python 中检索商店列表的函数,这个函数被称为:
class LeclercScraper(BaseScraper):
"""
This class allows scraping of Leclerc Drive website. It is the entry point for dataretrieval.
"""
def __init__(self):
LeclercDatabaseHelper = LeclercParser
super(LeclercScraper, self).__init__('http://www.leclercdrive.fr/', LeclercCrawler, LeclercParser, LeclercDatabaseHelper)
def get_list_stores(self, code):
"""
This method gets a list of stores given an area code
Input :
- code (string): from '01' to '95'
Output :
- stores :
[{
'name': '...',
'url'
}]
"""
when i try to write get_list_stores(92)
i get this error :
当我尝试编写时,出现get_list_stores(92)
此错误:
get_list_stores(92)
TypeError: get_list_stores() takes exactly 2 arguments (1 given)
how can you help me with this ?
你怎么能帮我解决这个问题?
采纳答案by óscar López
If the function is insidea 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:
如果它在类之外,则不带self
参数写入:
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)
回答by jsbueno
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
不要随意使用“self” - 建议将 self 作为编写为类中方法的函数的第一个参数。在这种情况下,当它作为方法被调用时,就像在
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)
Python 将在调用时自动插入“self”参数(它将是外部作用域中名为“a”的对象)
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.
在类定义之外,将第一个参数命名为“self”并没有多大意义——尽管它不是关键字,因此本身并不是错误。
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.
在您的情况下,您尝试调用的函数很可能是在类中定义的:您必须将其作为类实例的属性调用,然后您只需省略第一个参数 - 就像上面的示例一样.
回答by óscar López
If you are trying to use it in the class, access it like this:
如果您尝试在课堂上使用它,请像这样访问它:
self.get_listscores(92)
If you are trying to access it outside of the class, you need to first create an instance of LeclercScraper
:
如果您试图在类之外访问它,则需要首先创建一个实例LeclercScraper
:
x = LeclercScraper()
y = x.get_listscores(92)
Also, self
is not a keyword. It is simply the name chosen by convention to represent a class instance within itself.
此外,self
不是关键字。它只是按照惯例选择的名称来表示其自身内的类实例。
Here's a good reference:
这是一个很好的参考: