关于 import 模块

2017-08-30  本文已影响0人  Phoenix的学习历程
#codeing:utf-8
#! /usr/bin/python
#coding:utf-8

class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score

第一次运行的python程序,

>>> import Student
>>> student = Student("michael", 99)

发生了错误,

raceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

原因是python的import语法,在使用时需要加上模块名限定才能正确调用,

>>> student = Student.Student("michael", 99)
>>> student.name
'michael'

如果不想加上模块名,可以这么导入

>>> from Student import Student
>>> student = Student("michael", 98)
>>> student.name
'michael'
>>> student.score
98
上一篇 下一篇

猜你喜欢

热点阅读