什么在python ruby​​的加载中实现了相同的功能

时间:2012-12-07 15:21:28

标签: python ipython bpython

irb 中我们可以:

>> load 'example.rb'

example.rb的来源加载到环境中。

什么是bpython和ipython的替代方案?

2 个答案:

答案 0 :(得分:1)

假设你的PYTHONPATH中存在一个名为example.py的文件(很像ruby的$ LOAD_PATH)

在普通的python中:Details here

>>> import example  # Import module. 
>>> example.hello()  # Run code
hello

# Then, lets say you change the hello function to say "hello world, I'm changed!"

>>> reload(example)
<module 'example' from 'example.pyc'> 

>>> example.hello()
hello world, I changed!

IPython通过其他方式增加了上述所有内容。

  1. dreload就像重新加载一样,但递归地重新加载了example.py导入的模块。这意味着如果exam​​ple.py依赖于example2.py并且您更改了example2.py,则example.example2将反映更新的更改

    In [5]: dreload(example)  # (after import, of course)
    
    In [6]: dreload?  # Details...
    
  2. %run magic是我最喜欢的,因为它执行调用的文件,然后将该文件的命名空间嵌入到当前会话中。它意味着在调用时重新加载和重载,并且也像import *一样工作。我会说它最像红宝石的“负载”。

    In [1]: %run example.py
    In [2]: hello()
    hello
    
    # Make some changes to code
    
    In [3]: %run example.py
    hello world, I changed!
    
    In [4]: %run?  # Details...
    

答案 1 :(得分:0)

from this import *

this.py的内容加载到您当前的命名空间中。