【发布时间】:2010-04-22 14:20:56
【问题描述】:
我正在通过http://www.sthurlow.com/python/lesson08/ 的在线教程运行,并且我相信我至少在某种程度上了解类在 python 中的工作原理,但是当我运行此代码时:
class Shape:
def __init__(self,x,y):
self.x = x
self.y = y
description = "This shape has not been described yet"
author = "Nobody has claimed to make this shape yet"
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self,text):
self.description = text
def authorName(self,text):
self.author = text
def scaleSize(self,scale):
self.x = self.x * scale
self.y = self.y * scale
我收到此错误:
Traceback (most recent call last):
File "Y:/python/Shape.py", line 1, in -toplevel-
class Shape:
File "Y:/python/Shape.py", line 17, in Shape
self.y = self.y * scale
NameError: name 'self' is not defined
任何帮助都会很棒
谢谢
理查德
【问题讨论】:
-
我不是 python 专家(所以我帮不上忙),但我建议在编辑器中为您的代码使用代码块,尤其是因为 Python 依赖于缩进。然后人们将能够准确地看到你可能做错了什么。
-
@Richie: 啊,很好 :) 希望我能做到。