解决方案 Brian Blazer写道:好的,我这里有一个非常简单的课: 班级学生:"""定义学生班级"" def __init __(self,lName,fName,mi): self.lName = lName self.fName = fName self.mi = mi 然后我有一个小脚本,我用作测试: 来自学生导入* s1 =学生(Brian,Smith,N) print s1.lName 这可以按预期工作。但是,如果我将import语句更改为:导入学生 我收到错误: TypeError:''module''对象不可调用 我认为你收到错误就行了 s1 =学生(Brian,Smith,N) 这是因为当你使用''import Student'时,它会加载文件 将Student.py放入名为Student的名称空间中(与''from'' 语句不同,后者将其加载到主命名空间中)。从学生模块访问任何 ,以学生为前缀。 ,所以你的行变为: s1 = Student.Student(Brian,Smith,N) Iain谢谢 - 感谢您的时间。 Brian br***@brianandkate.com >我试图查看发生了什么,但我还没有找到任何东西。有人可以花一点时间给出解释吗? 来自< b $ b来自< ;模块> import< * | nameslist> 语法导入< module>中的部分或全部名称进入当前模块 名称空间。因此,你可以访问你的班级。 但如果你这样做 import< module> 你只得到< module>在当前的命名空间中。所以你需要访问 里面的任何内容< module>通过为表达式添加前缀。在你的情况下,它是 Student.Student 如果你只写学生,那实际上是MODULE学生,其中/> 解释了错误消息。 现在这听起来好像来自< module>导入*语法是 去的方式,你应该克制,直到你真的知道你在做什么 (你现在_don''t_不知道),如这可能会引入微妙的和难以调试的错误。如果你不想写长模块名,你可以用代替它们: import< moduel-with-long-name> as< shortname> 看起来好像你有一些JAVA背景,把一个类放在一个叫做同类的文件中。不要这样做,这是JAVA中的一个愚蠢的限制 ,应该避免在PYTHON中使用。 Diez 感谢您的回复。我有一种感觉是有事可做 有命名空间问题但是我不确定。 你是对的,我来自一个Java背景。如果它的形式很差 将您的班级文件命名为与您的班级相同,我可以问一下 标准是什么吗? 再次感谢, Brian 2006年5月19日上午8:33,Diez B. Roggisch写道: 我试图查看发生的事情,但我还没有发现任何事情。是否可以让某人花一分钟时间给出解释? 来自< module>的 import< * | nameslist> 语法导入< module>中的部分或全部名称进入当前模块的命名空间。因此,你可以访问你的班级。 但如果你这样做 导入< module> 你只能获得< module>在当前的命名空间中。所以你需要访问里面的任何内容< module>通过为表达式添加前缀。在你的情况下, 学生。学生 如果你只写学生,那实际上是MODULE学生,它解释了错误信息。 现在这听起来好像来自< module>导入*语法是去的方式,你应该克制,直到你真正知道你在做什么(你现在_don''t_不知道),这可能会引入微妙且难以调试的错误。如果您不想写长模块名称,那么您可以为它们添加别名: import< moduel-with-long-name> as< shortname> 看起来好像你有一些JAVA背景,把一个类放在一个名为和类相同的文件中。不要这样做,在JAVA中这是一个愚蠢的限制,在PYTHON中应该避免。 Diez - http://mail.python.org/mailman/listinfo/python-list OK, I have a very simple class here:class Student:"""Defines the student class"""def __init__(self, lName, fName, mi):self.lName = lNameself.fName = fNameself.mi = miThen I have a small script that I am using as a test:from Student import *s1 = Student("Brian", "Smith", "N")print s1.lNameThis works as expected. However, if I change the import statement to:import StudentI get an error:TypeError: ''module'' object is not callableI have tried to look up what is going on, but I have not foundanything. Would it be possible for someone to take a minute and givean explanation?Thank you - your time is appreciated.Brian br***@brianandkate.com 解决方案Brian Blazer wrote: OK, I have a very simple class here: class Student: """Defines the student class""" def __init__(self, lName, fName, mi): self.lName = lName self.fName = fName self.mi = mi Then I have a small script that I am using as a test: from Student import * s1 = Student("Brian", "Smith", "N") print s1.lName This works as expected. However, if I change the import statement to: import Student I get an error: TypeError: ''module'' object is not callable I have tried to look up what is going on, but I have not found anything. Would it be possible for someone to take a minute and give an explanation?I take it you are getting the error on the lines1 = Student("Brian", "Smith", "N")This is because when you use ''import Student'', it loads the fileStudent.py into a namespace called Student (unlike the ''from''statement, which loads it into the main namespace). to access anythingfrom your Student module, prepend with Student. , so your line becomes:s1 = Student.Student("Brian", "Smith", "N")Iain Thank you - your time is appreciated. Brian br***@brianandkate.com > I have tried to look up what is going on, but I have not found anything. Would it be possible for someone to take a minute and give an explanation?Thefrom <module> import <*|nameslist>syntax imports some or all names found in <module> into the current modulesnamespace. Thus you can access your class.But if you doimport <module>you only get <module> in your current namespace. So you need to accessanything inside <module> by prefixing the expression. In your case, it isStudent.StudentIf you only write Student, that in fact is the MODULE Student, whichexplains the error message.Now while this sounds as if the from <module> import * syntax is the way togo, you should refrain from that until you really know what you are doing(and you currently _don''t_ know), as this can introduce subtle anddifficult to debug bugs. If you don''t want to write long module-names, youcan alias them:import <moduel-with-long-name> as <shortname>And it seems as if you have some JAVA-background, putting one class in onefile called the same as the class. Don''t do that, it''s a stupid restrictionin JAVA and should be avoided in PYTHON.DiezThank you for your responses. I had a feeling is had something to dowith a namespace issue but I wasn''t sure.You are right, I do come from a Java background. If it is poor formto name your class file the same as your class, can I ask what thestandard is?Thanks again,BrianOn May 19, 2006, at 8:33 AM, Diez B. Roggisch wrote: I have tried to look up what is going on, but I have not found anything. Would it be possible for someone to take a minute and give an explanation? The from <module> import <*|nameslist> syntax imports some or all names found in <module> into the current modules namespace. Thus you can access your class. But if you do import <module> you only get <module> in your current namespace. So you need to access anything inside <module> by prefixing the expression. In your case, it is Student.Student If you only write Student, that in fact is the MODULE Student, which explains the error message. Now while this sounds as if the from <module> import * syntax is the way to go, you should refrain from that until you really know what you are doing (and you currently _don''t_ know), as this can introduce subtle and difficult to debug bugs. If you don''t want to write long module- names, you can alias them: import <moduel-with-long-name> as <shortname> And it seems as if you have some JAVA-background, putting one class in one file called the same as the class. Don''t do that, it''s a stupid restriction in JAVA and should be avoided in PYTHON. Diez -- http://mail.python.org/mailman/listinfo/python-list 这篇关于noob导入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!