[关闭]
@Channelchan 2018-03-21T10:07:58.000000Z 字数 2465 阅读 135993

Python模块导入

import

想使用Python源文件,只需在另一个源文件里执行import语句,语法如下:

  1. import numpy
  2. n=numpy.array([[1, 2],[3, 4]])
  3. print(n)
[[1 2]
 [3 4]]
  1. import matplotlib.pyplot as plt
  2. plt.plot(n)
  3. plt.show()

output_2_0.png-10.8kB

from...import*

这提供了一个简单的方法来导入一个模块中的所有项目。然而这种声明不该被过多地使用。
把一个模块的所有内容全都导入到当前的命名空间也是可行的,只需使用如下声明:

  1. from datetime import *

from...import语句:

Python的from语句让你从模块中导入一个指定的部分到当前命名空间中。

dir()函数一个排好序的字符串列表,返回的列表容纳了在一个模块里定义的所有模块,变量和函数。

  1. from datetime import time
  2. print(dir(time))
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dst', 'fold', 'hour', 'isoformat', 'max', 'microsecond', 'min', 'minute', 'replace', 'resolution', 'second', 'strftime', 'tzinfo', 'tzname', 'utcoffset']
  1. print(help(time))
Help on class time in module datetime:

class time(builtins.object)
 |  time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object
 |  
 |  All arguments are optional. tzinfo may be None, or an instance of
 |  a tzinfo subclass. The remaining arguments may be ints.
 |  
 |  Methods defined here:
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __format__(...)
 |      Formats self with strftime.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __reduce__(...)
 |      __reduce__() -> (cls, state)
 |  
 |  __reduce_ex__(...)
 |      __reduce_ex__(proto) -> (cls, state)
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __str__(self, /)
 |      Return str(self).
 |  
 |  dst(...)
 |      Return self.tzinfo.dst(self).
 |  
 |  isoformat(...)
 |      Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].
 |      
 |      timespec specifies what components of the time to include.
 |  
 |  replace(...)
 |      Return time with new specified fields.
 |  
 |  strftime(...)
 |      format -> strftime() style string.
 |  
 |  tzname(...)
 |      Return self.tzinfo.tzname(self).
 |  
 |  utcoffset(...)
 |      Return self.tzinfo.utcoffset(self).
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  fold
 |  
 |  hour
 |  
 |  microsecond
 |  
 |  minute
 |  
 |  second
 |  
 |  tzinfo
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  max = datetime.time(23, 59, 59, 999999)
 |  
 |  min = datetime.time(0, 0)
 |  
 |  resolution = datetime.timedelta(0, 0, 1)

None
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注