Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python对象中使用self参数_Python_Class - Fatal编程技术网

在python对象中使用self参数

在python对象中使用self参数,python,class,Python,Class,我有一个关于在python中定义函数和self参数的问题 有以下代码 class Dictionaries(object): __CSVDescription = ["ID", "States", "FilterTime", "Reaction", "DTC", "ActiveDischarge"] def __makeDict(Lst): return dict(zip(Lst, range(len(Lst)))) def getDict(self)

我有一个关于在python中定义函数和self参数的问题

有以下代码

class Dictionaries(object):
    __CSVDescription = ["ID", "States", "FilterTime", "Reaction", "DTC", "ActiveDischarge"]

    def __makeDict(Lst):
        return dict(zip(Lst, range(len(Lst))))

    def getDict(self):
        return self.__makeDict(self.__CSVDescription)

    CSVDescription = __makeDict(__CSVDescription)

x = Dictionaries()
print x.CSVDescription
print x.getDict()
x.CSVDescription
工作正常。但是
print x.getDict()
返回一个错误

TypeError: __makeDict() takes exactly 1 argument (2 given)
我可以将
self
-参数添加到
\u makeDict()
方法中,但是
print x.CSVDescription
不起作用


如何正确使用
self
-参数?

在python中,
self
参数隐式传递给实例方法,除非该方法用
@staticmethod
修饰

在这种情况下,
\uu makeDict
不需要对对象本身的引用,因此可以将其设置为静态方法,这样您就可以省略
self

@staticmethod
def __makeDict(Lst): # ...

def getDict(self):
    return self.__makeDict(self.__CSVDescription)

在python中,
self
参数隐式传递给实例方法,除非该方法用
@staticmethod
修饰

在这种情况下,
\uu makeDict
不需要对对象本身的引用,因此可以将其设置为静态方法,这样您就可以省略
self

@staticmethod
def __makeDict(Lst): # ...

def getDict(self):
    return self.__makeDict(self.__CSVDescription)

使用
@staticmethod
的解决方案在这里不起作用,因为从类主体本身调用方法不会调用描述符协议(如果普通方法是描述符,这也会是一个问题,但在编译类定义之前,情况并非如此)。这里有四个主要选项,但其中大多数都可能被视为某种程度的代码混淆,并且需要一个注释来回答“为什么不使用
静态方法
?”的问题

第一个是,正如@Marcus所建议的,总是从类而不是从实例调用方法。也就是说,每次你做
self.\uu makeDict
,都要做
self.\uuu class.\uuuu\uu makeDict
。这看起来很奇怪,因为这是一件奇怪的事情——在Python中,几乎不需要将方法调用为
Class.method
,而且只有在使用
super
之前编写的代码时,才使用
self.\uu Class\uuu
是错误的

类似地,但反过来说,您可以将其设置为一个
staticmethod
,并在类主体中手动调用描述符协议-do:
\uuuu makeDict.\uuu get\uuuuu(无,字典)(\uu lst)

或者,您可以通过使用可选参数来检测调用它的上下文:

def __makeDict(self, Lst=None):
    if Lst is None:
       Lst = self
    ...
但是,到目前为止,最好的方法是认识到您使用的是Python,而不是Java——将其放在类之外

def _makeDict(Lst):
    ...

class Dictionaries(object):
   def getDict(self):
      return _makeDict(self.__CSVDescription)

   CSVDescription = _makeDict(__CSVDescription)

使用
@staticmethod
的解决方案在这里不起作用,因为从类主体本身调用方法不会调用描述符协议(如果普通方法是描述符,这也会是一个问题,但在编译类定义之前,情况并非如此)。这里有四个主要选项,但其中大多数都可能被视为某种程度的代码混淆,并且需要一个注释来回答“为什么不使用
静态方法
?”的问题

第一个是,正如@Marcus所建议的,总是从类而不是从实例调用方法。也就是说,每次你做
self.\uu makeDict
,都要做
self.\uuu class.\uuuu\uu makeDict
。这看起来很奇怪,因为这是一件奇怪的事情——在Python中,几乎不需要将方法调用为
Class.method
,而且只有在使用
super
之前编写的代码时,才使用
self.\uu Class\uuu
是错误的

类似地,但反过来说,您可以将其设置为一个
staticmethod
,并在类主体中手动调用描述符协议-do:
\uuuu makeDict.\uuu get\uuuuu(无,字典)(\uu lst)

或者,您可以通过使用可选参数来检测调用它的上下文:

def __makeDict(self, Lst=None):
    if Lst is None:
       Lst = self
    ...
但是,到目前为止,最好的方法是认识到您使用的是Python,而不是Java——将其放在类之外

def _makeDict(Lst):
    ...

class Dictionaries(object):
   def getDict(self):
      return _makeDict(self.__CSVDescription)

   CSVDescription = _makeDict(__CSVDescription)

但是当我添加
@staticmethod
时,
CSVDescription=\uu makeDict(\uu CSVDescription)
不起作用。然后我得到一个错误
TypeError:“staticmethod”对象不可调用
@wewa您使用的是哪个Python版本?我使用的是Python版本2.5.1,但是
CSVDescription=\uu makeDict(\uu CSVDescription)
在我添加
@staticmethod
时不起作用。然后我得到错误
TypeError:“staticmethod”对象不可调用
@wewa您使用的是哪种Python版本?我使用的是Python版本2.5.1是的,您是对的,我犯了错误并删除了我的答案。是的,您是对的,我犯了错误并删除了我的答案。