如何使用从模块导入的对象?

2024-10-08 18:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用模块中的类时遇到问题。你知道吗

模块:http://pastebin.com/A3UQ2Ppy

def ClassName(object)
    def __init__(self, var):
        self.var = var

    def method(self):
        print 'big success'

脚本:http://pastebin.com/BqVthvG0

import module
object = module.ClassName(1337)
print object.method()

这基本上就是我在代码中所做的,我得到了一个错误: AttributeError:'NoneType'对象没有属性'method'


Tags: 模块selfcomhttpobjectinitvardef
1条回答
网友
1楼 · 发布于 2024-10-08 18:31:21

您没有定义类,而是定义了一个函数:

def Network(object):
    def __init__(self, dimensions):
        self.dimensions = dimensions

让它成为一门课:

class Network(object):
    def __init__(self, dimensions):
        self.dimensions = dimensions

下一个例外是,您不能分配给集合:

self.nodes = {'all'}
for i in range(dimensions):
    self.nodes['dimension '+str(i)] = []

你可能想self.nodes成为一本字典:

self.nodes = {'all': []}

相关问题 更多 >

    热门问题