通常将所有方法的第一个参数称为
self。它指的是为其调用方法的实例。
假设您有:
class A(object): def foo(self): print 'Foo' def bar(self, an_argument): print 'Bar', an_argument
然后,执行以下操作:
a = A()a.foo() #prints 'Foo'a.bar('Arg!') #prints 'Bar Arg!'调用并没有什么特别的
self,您可以执行以下操作:
class B(object): def foo(self): print 'Foo' def bar(this_object): this_object.foo()
然后,执行以下操作:
b = B()b.bar() # prints 'Foo'
在您的特定情况下:
dangerous_device = MissileDevice(some_battery)dangerous_device.move(dangerous_device.RIGHT)
(如评论中所建议,
MissileDevice.RIGHT此处可能更合适!)
您 可以 在模块级别声明所有常量,因此可以执行以下操作:
dangerous_device.move(RIGHT)
但是,这将取决于您希望代码的组织方式!



