python可以使用没有"self"作为第一个参数的类或实例方法吗?

bob*_*nto 4 python oop methods

可能重复:
为什么需要在Python方法中明确地使用"self"参数?
Python'self'解释道

这只是为了我自己的启发.我正在学习python并且已经使用python进入OOP.我见过的类中每个方法的例子都有"self"作为第一个参数.所有方法都是如此吗?如果确实如此,那么python是否已被编写,以至于这个参数只是被理解,因此不需要?谢谢.

aba*_*ert 15

如果您想要一个不需要访问的方法self,请使用staticmethod:

class C(object):
    def my_regular_method(self, foo, bar):
        pass
    @staticmethod
    def my_static_method(foo, bar):
        pass

c = C()
c.my_regular_method(1, 2)
c.my_static_method(1, 2)
Run Code Online (Sandbox Code Playgroud)

如果要访问,而不是实例,请使用classmethod:

class C(object):
    @classmethod
    def my_class_method(cls, foo, bar):
        pass

c.my_class_method(1, 2)    
Run Code Online (Sandbox Code Playgroud)