我有以下类,我想location_change在__init__该类中使用该方法.有没有办法做到这一点?我在下面使用的示例是一个简化,但我需要做的是使用类方法来转换一些构造函数数据.这可以在python中完成吗?
class dummy:
def __init__(self):
self.location = 'USA'
print(self.location)
location_change()
print(self.location)
def location_change():
self.location = 'UK'
first_dummy = dummy()
Run Code Online (Sandbox Code Playgroud)
当然可以!
self.location_change()
Run Code Online (Sandbox Code Playgroud)
类中的每个方法都应该至少使用一个参数,这通常称为self.
def location_change(self):
Run Code Online (Sandbox Code Playgroud)
关于oop in python的入门教程 http://www.voidspace.org.uk/python/articles/OOP.shtml
文档 http://docs.python.org/tutorial/classes.html