noob导入问题 [英] noob import question

查看:112
本文介绍了noob导入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我这里有一个非常简单的课程:


class学生:

"""定义学生班级"" ;


def __init __(self,lName,fName,mi):

self.lName = lName

self.fName = fName

self.mi = mi


然后我有一个小脚本,我用作测试:

$ b来自学生进口的$ b *


s1 =学生(Brian,Smith,N)


打印s1.lName


这按预期工作。但是,如果我将import语句更改为:

import Student


我收到错误:

TypeError:''module ''对象不可调用


我试图查看发生了什么,但我还没有找到

任何东西。有人可以花点时间给你一个解释吗?


谢谢 - 感谢您的时间。

Brian
br***@brianandkate.com

解决方案



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
-
< a rel =nofollowhref =http://mail.python.org/mailman/listinfo/python-listtarget =_ blank> 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 = 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?

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 line
s1 = Student("Brian", "Smith", "N")

This is because when you use ''import Student'', it loads the file
Student.py into a namespace called Student (unlike the ''from''
statement, which loads it into the main namespace). to access anything
from 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?



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


Thank you for your responses. I had a feeling is had something to do
with a namespace issue but I wasn''t sure.

You are right, I do come from a Java background. If it is poor form
to name your class file the same as your class, can I ask what the
standard is?

Thanks again,
Brian

On 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导入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆