在 python 中导入类型
import types in python
我有一个名为 types
的模块,其中包含以下代码
def Hello():
return "Hello World"
但是当我这样做的时候
>>> import types
>>> types.Hello()
它给了我
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Hello'
这真是把我吓坏了。
types
是内置模块,import
先搜索内置模块,再查看本地包。
>>> import types
>>> types
<module 'types' from '/usr/lib/python2.7/types.pyc'>
尝试
>>> from . import types
实际上 types
是一个内置模块,它不提供 Hello
function.If 你想创建一个服装模块,你需要把它放在你的 python 目录!
请注意,您的模块名称不应与 python 内置模块相同!
切勿将您的模块命名为与 Python 内置模块相同,阅读this section and Naming Convetion 以获得更好的想法。
4.8. Intermezzo: Coding Style
For Python, PEP 8 has emerged as the style guide that most projects
adhere to; it promotes a very readable and eye-pleasing coding style.
Every Python developer should read it at some point; here are the most
important points extracted for you:
Name your classes and functions consistently; the convention is to use
CamelCase for classes and lower_case_with_underscores for functions
and methods. Always use self as the name for the first method argument
(see A First Look at Classes for more on classes and methods).
刚刚从这个部分发布了与您的问题相关的内容。
我有一个名为 types
的模块,其中包含以下代码
def Hello():
return "Hello World"
但是当我这样做的时候
>>> import types
>>> types.Hello()
它给了我
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Hello'
这真是把我吓坏了。
types
是内置模块,import
先搜索内置模块,再查看本地包。
>>> import types
>>> types
<module 'types' from '/usr/lib/python2.7/types.pyc'>
尝试
>>> from . import types
实际上 types
是一个内置模块,它不提供 Hello
function.If 你想创建一个服装模块,你需要把它放在你的 python 目录!
请注意,您的模块名称不应与 python 内置模块相同!
切勿将您的模块命名为与 Python 内置模块相同,阅读this section and Naming Convetion 以获得更好的想法。
4.8. Intermezzo: Coding Style
For Python, PEP 8 has emerged as the style guide that most projects adhere to; it promotes a very readable and eye-pleasing coding style. Every Python developer should read it at some point; here are the most important points extracted for you:
Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).
刚刚从这个部分发布了与您的问题相关的内容。