0

我使用了以下代码

class FooBar:
    def __init__(self):
        self.x = 0
        self.y = 0

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

我将以上内容保存在FooBar.py

当我使用时,

import FooBar
p = FooBar() 

错误说module object not callable。是什么原因?

4

2 回答 2

1

第二种def重新声明__init__方法。在 python 中,你不能重载方法。

于 2013-11-03T13:46:16.857 回答
1

因为FooBar您要导入的是模块,而不是类。

代替:

import FooBar

和:

from FooBar import FooBar

或者,如果您愿意,可以使用:

import FooBar
# First FooBar is the module, second is the class within the module.
p = FooBar.FooBar() 
于 2013-11-03T13:46:40.587 回答