关于 import 模块

#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 "", line 1, in 
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

你可能感兴趣的:(关于 import 模块)