def save_file(self, outputfilename = self.image_filename):
self.file.read(outputfilename)
....
第一行报错 NameError: name 'self' is not defined
。看起来 Python 不接受这个。我该怎么改写代码以避免抛出异常?
def save_file(self, outputfilename = self.image_filename):
self.file.read(outputfilename)
....
第一行报错 NameError: name 'self' is not defined
。看起来 Python 不接受这个。我该怎么改写代码以避免抛出异常?
使用默认值 None
并进行检测。
def save_file(self, outputfilename=None):
if outputfilename is None:
outputfilename = self.image_filename
self.file.read(outputfilename)
....
def save_file(self, outputfilename=None):
outputfilename = outputfilename or self.image_filename
self.file.read(outputfilename)
def save_file(self, outputfilename=None):
self.file.read(outputfilename or self.image_filename)
对于一个变量来说,这可能并不算什么,但如果你有5个变量,这可以让代码更易读,我认为。
def save_file(self, outputfilename = None):
if not outputfilename:
outputfilename = self.image_filename
self.file.read(outputfilename)
....