小编Ear*_*arl的帖子

Python 模块和类 - AttributeError: 模块没有属性

我是 python 的新手,我正在尝试创建一个模块和类。

如果我尝试导入mystuff然后使用cfcpiano = mystuff.Piano(),则会出现错误:

AttributeError: module 'mystuff' has no attribute 'Piano'
Run Code Online (Sandbox Code Playgroud)

如果我尝试从mystuff import Piano我得到:

ImportError: cannot import name 'Piano'
Run Code Online (Sandbox Code Playgroud)

有人可以解释发生了什么吗?如何在 Python 中使用模块和类

mystuff.py

def printhello():
    print ("hello")

def timesfour(input):
    print (input * 4)


class Piano:
    def __init__(self):
        self.type = raw_input("What type of piano? ")

    def printdetails(self):
        print (self.type, "piano, " + self.age)
Run Code Online (Sandbox Code Playgroud)

测试文件

import mystuff 
from mystuff import Piano 
cfcpiano = mystuff.Piano()
cfcpiano.printdetails()
Run Code Online (Sandbox Code Playgroud)

python module class

5
推荐指数
1
解决办法
7737
查看次数

标签 统计

class ×1

module ×1

python ×1