与 Python 中的 OOP 混淆
Confusion With OOP in Python
我正在尝试学习 Python 中的 OOP,但我对某些部分感到困惑。
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print line
def print_x(self):
print x
happy_bday = Song(["Happy birthday to you,",
"I don't want to get sued",
"So I'll stop right there"])
bulls_on_parade = Song(["They'll rally around the family",
"With pockets full of shells"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
x = Song(4)
x.print_x()
print_x() returns:
<__main__.Song object at 0x7f00459b4390>
而不是 4。所以我尝试将 x 添加到 __init__
和 print_x
的参数中,并在 print_x
函数中将打印 x 更改为打印 self.x 加上添加 self.x
= x 到 init,但它 returns this:
TypeError: __init__() takes exactly 3 arguments (2 given)
老实说,我不知道这里出了什么问题。但是任何帮助都会对我最终理解 OOP 非常有益。
我认为你正在尝试
def __init__(self, lyrics, x):
self.lyrics = lyrics
self.x = x
...
def print_x(self):
print self.x
通过这种方式,它将产生 TypeError: init() takes exactly 3 arguments (2 given)
你可以从这里找到错误。
当您创建 Song
实例时
happy_bday = Song(["Happy birthday to you,",
"I don't want to get sued",
"So I'll stop right there"])
您必须将 x
的值传递给 __init__()
。这就是错误 showing.This 可以由
完成的原因
happy_bday = Song(["Happy birthday to you,",
"I don't want to get sued",
"So I'll stop right there"],
'x-value')
或
为 x
设置默认值
def __init__(self, lyrics, x="default value"):
self.lyrics = lyrics
self.x = x
这与其说是一个 OOP 问题,不如说是一个范围界定问题。让我们检查一个非常精简的版本。
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def print_x(self):
print x
从这里我们实例化x
(在本地范围内):
>>> x = Song(4)
现在,在我们做任何事情之前,让我们检查一下 x
:
>>> print x.lyrics
4
原因是当你调用Song(4)
时,值4被它的位置确定为lyrics
。
当我们调用 print_x
时:
>>> x.print_x()
<__main__.Song object at 0x7f00459b4390> # Or some other memory address
原因是 Python 知道的唯一 x
是我们刚刚创建的本地 x
。
当我们重新开始时会发生什么y
:
>>> y = Song(4)
>>> print y.print_x ()
NameError: global name 'x' is not defined
不存在要打印的 x
,它会抛出异常。
我正在尝试学习 Python 中的 OOP,但我对某些部分感到困惑。
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print line
def print_x(self):
print x
happy_bday = Song(["Happy birthday to you,",
"I don't want to get sued",
"So I'll stop right there"])
bulls_on_parade = Song(["They'll rally around the family",
"With pockets full of shells"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
x = Song(4)
x.print_x()
print_x() returns:
<__main__.Song object at 0x7f00459b4390>
而不是 4。所以我尝试将 x 添加到 __init__
和 print_x
的参数中,并在 print_x
函数中将打印 x 更改为打印 self.x 加上添加 self.x
= x 到 init,但它 returns this:
TypeError: __init__() takes exactly 3 arguments (2 given)
老实说,我不知道这里出了什么问题。但是任何帮助都会对我最终理解 OOP 非常有益。
我认为你正在尝试
def __init__(self, lyrics, x):
self.lyrics = lyrics
self.x = x
...
def print_x(self):
print self.x
通过这种方式,它将产生 TypeError: init() takes exactly 3 arguments (2 given)
你可以从这里找到错误。
当您创建 Song
实例时
happy_bday = Song(["Happy birthday to you,",
"I don't want to get sued",
"So I'll stop right there"])
您必须将 x
的值传递给 __init__()
。这就是错误 showing.This 可以由
happy_bday = Song(["Happy birthday to you,",
"I don't want to get sued",
"So I'll stop right there"],
'x-value')
或
为 x
def __init__(self, lyrics, x="default value"):
self.lyrics = lyrics
self.x = x
这与其说是一个 OOP 问题,不如说是一个范围界定问题。让我们检查一个非常精简的版本。
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def print_x(self):
print x
从这里我们实例化x
(在本地范围内):
>>> x = Song(4)
现在,在我们做任何事情之前,让我们检查一下 x
:
>>> print x.lyrics
4
原因是当你调用Song(4)
时,值4被它的位置确定为lyrics
。
当我们调用 print_x
时:
>>> x.print_x()
<__main__.Song object at 0x7f00459b4390> # Or some other memory address
原因是 Python 知道的唯一 x
是我们刚刚创建的本地 x
。
当我们重新开始时会发生什么y
:
>>> y = Song(4)
>>> print y.print_x ()
NameError: global name 'x' is not defined
不存在要打印的 x
,它会抛出异常。