Python和Ruby交互式shell的区别 [英] Difference in Python and Ruby interactive shells

查看:106
本文介绍了Python和Ruby交互式shell的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python和Ruby的交互式解释器环境,我遇到了似乎是基本的

差异。但是我可能在Python中做错了。如果我错了,请

评论并纠正我


在这两种语言中,你都可以启动互动翻译

(' 'python''和''irb''),加载源文件并做一些事情,比如创建

对象并调用它们的方法。当你想要改变某些东西时,你可以在环境之外编辑那些相同的源文件,并在交互式环境中重新加载它们。但是,这是

的区别:使用Python,当你重新加载源文件(模块在

Python术语中)时,你的现有变量似乎仍然与<来自旧版本模块的
实现。在Ruby中,在重新加载

之后,您现有的变量指向新的实现。至少
,这就是类及其方法所发生的情况。这意味着在Python中,如果你有一个交互式构建的应用程序,

引用对象,指向对象的数据结构等等,你就是


必须重新构建该应用程序以获得新的

实现。使用Ruby,当你加载新的实现时,你就可以立即得到它。


这是一个简单的例子:


----------------------------------------------- --------------------------------

Python交互式shell(python.exe)


C:\ home \dh0072 \ rq> python

Python 2.4.2(#67,2005年9月28日,12:41:11)[ MSC v.1310 32位(英特尔)]
win32上的



#加载本地文件b.py进行测试

I am experimenting with the interactive interpreter environments of
Python and Ruby and I ran into what seems to be a fundamental
difference. However I may be doing something wrong in Python. Please
comment and correct me if I am wrong

In both languages, you can start up the interactive interpreter
(''python'' and ''irb''), load source files and do stuff, like create
objects and call their methods. When you want to change something, you
can edit those same source files outside the environment and reload
them from within the interactive environment. But, here is the
difference: with Python, when you reload the source file (module in
Python terms), it seems that your existing variables stay bound to the
implementations from the old version of the module. In Ruby, after a
reload, your existing variables point to the new implementation. At
least, that is what happens with classes and their methods. This means
that in Python, if you have an application built up interactively, with
references to objects, data structures pointing to objects, etc., you
would have to reconstruct that application to get the new
implementation. With Ruby, when you load the new implementation, you
get it immediately.

here is a simple example:

-------------------------------------------------------------------------------
Python interactive shell (python.exe)

C:\home\dh0072\rq>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

# load a local file b.py for test

import b


#实例化对象并调用方法

x = bB()
xp()
B


#编辑b.py离线以更改方法''p''做一些不同的事情

reload

reload( b)
xp()
B


#变量''x'的绑定不会改变。指向旧版本

y = bB()
yp()
BBBBB


#'b'点的新实例到新版本
import b
# instantiate an object and call a method
x = b.B()
x.p() B

# edit b.py offline to change method ''p'' to do something different then
reload
reload(b) x.p() B

# binding of variable ''x'' IS NOT changed. points to old version
y = b.B()
y.p() BBBBB

# new instance of ''b'' points to new version



----------------------- -------------------------------------------------- ------

Ruby交互式shell(irb.exe)


C:\ home \dh0072 \ rq> irb


#加载本地文件b.py进行测试


irb(main):001:0>加载b.rb

=> true


#实例化一个对象并调用一个方法


irb(main):002:0> x = B.new

=> #

irb(主要):003:0> x.p

B

=>没有


#编辑b.py离线改变方法''p''做一些不同的事情然后

重新加载


irb(main):004:0>加载b.rb

=> true

irb(主要):005:0> x.p

BBBB

=> nil


#变量x的绑定被改变了。指向新版本


irb(主要):006:0> y = B.new

=> #

irb(主要):007:0> y.p

BBBB

=>没有


#'b'的新实例指向新版本


-------------------------------------------------------------------------------
Ruby interactive shell (irb.exe)

C:\home\dh0072\rq>irb

# load a local file b.py for test

irb(main):001:0> load "b.rb"
=> true

# instantiate an object and call a method

irb(main):002:0> x = B.new
=> #
irb(main):003:0> x.p
B
=> nil

# edit b.py offline to change method ''p'' to do something different then
reload

irb(main):004:0> load "b.rb"
=> true
irb(main):005:0> x.p
BBBB
=> nil

# binding of variable ''x'' IS changed. points to new version

irb(main):006:0> y = B.new
=> #
irb(main):007:0> y.p
BBBB
=> nil

# new instance of ''b'' points to new version

推荐答案

dmh2000写道:
dmh2000 wrote:
我正在尝试使用Python和Ruby的交互式解释器环境,我遇到了似乎是一个基本的差异。但是我可能在Python中做错了。如果我错了,请
评论并纠正我

在这两种语言中,你都可以启动互动翻译
(''python''和''irb''),加载源文件并做一些事情,比如创建
对象并调用它们的方法。当您想要更改某些内容时,您可以在环境之外编辑相同的源文件,并在交互式环境中重新加载它们。但是,这里有不同之处:使用Python,当你重新加载源文件(用Python术语中的模块)时,你的现有变量似乎仍然与旧版本的
实现绑定该模块。在Ruby中,在重新加载之后,您现有的变量指向新的实现。至少,这就是类和它们的方法所发生的事情。这意味着在Python中,如果你有一个交互式构建的应用程序,对对象的引用,指向对象的数据结构等,你将需要重建该应用程序才能获得新的
实施。使用Ruby,当您加载新实现时,您可以立即获得它。
我不完全确定这是你在说什么,但我的意思是,你忽略了那个

,在IDLE菜单项[Shell]中你有

选项重新启动shell会让你回到初始状态。

如果在Ruby中知道这件事我会很有意思

开始新运行时保留旧值的选项?


Claudio
这是一个简单的例子:

------- -------------------------------------------------- ----------------------
Python交互式shell(python.exe)

C:\ home \dh0072 \ rq> python
Python 2.4.2(#67,2005年9月28日,12:41:11)[MSC v.1310 32位(英特尔)]
关于win32
类型& ;帮助","版权"," credit"或许可证或更多信息。

#加载本地文件b.py进行测试

I am experimenting with the interactive interpreter environments of
Python and Ruby and I ran into what seems to be a fundamental
difference. However I may be doing something wrong in Python. Please
comment and correct me if I am wrong

In both languages, you can start up the interactive interpreter
(''python'' and ''irb''), load source files and do stuff, like create
objects and call their methods. When you want to change something, you
can edit those same source files outside the environment and reload
them from within the interactive environment. But, here is the
difference: with Python, when you reload the source file (module in
Python terms), it seems that your existing variables stay bound to the
implementations from the old version of the module. In Ruby, after a
reload, your existing variables point to the new implementation. At
least, that is what happens with classes and their methods. This means
that in Python, if you have an application built up interactively, with
references to objects, data structures pointing to objects, etc., you
would have to reconstruct that application to get the new
implementation. With Ruby, when you load the new implementation, you
get it immediately. I am not fully sure it is what you are speaking about, but I mean, that
you have overlooked, that in the IDLE menu item [Shell] you have the
option to restart the shell what takes you back to the initial state.
It would be interesting to know for me here, if there is in Ruby the
option to keep the old values when starting a new run?

Claudio
here is a simple example:

-------------------------------------------------------------------------------
Python interactive shell (python.exe)

C:\home\dh0072\rq>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

# load a local file b.py for test

import b

#实例化一个对象并调用一个方法

x = bB()
xp()
B

#edit b .py离线改变方法''p'做不同的事情然后重新加载

重装(b)
xp()
B
<变量''x'的#绑定未更改。指向旧版本

y = bB()
yp()
import b

# instantiate an object and call a method

x = b.B()
x.p()
B

# edit b.py offline to change method ''p'' to do something different then
reload

reload(b)
x.p()
B

# binding of variable ''x'' IS NOT changed. points to old version

y = b.B()
y.p()



BBBBB

# ''b'的新实例指向新版本

------------------------------ -------------------------------------------------
Ruby交互式shell(irb.exe)

C:\ home \dh0072 \ rq> irb

#加载本地文件b.py进行测试

irb(主要):001:0>加载b.rb
=>真实的
#实例化一个对象并调用一个方法

irb(main):002:0> x = B.new
=> #
irb(主要):003:0> x.p
B
=>没有

#edit b.py离线改变方法''p''做一些不同的事情然后重新加载

irb(主要):004:0>加载b.rb
=> true
irb(main):005:0> x.p
BBBB
=> nil

irb(主要):006:0> y = B.new
=> #
irb(主要):007:0> y.p
BBBB
=> nil

#'b'的新实例指向新版本



BBBBB

# new instance of ''b'' points to new version
-------------------------------------------------------------------------------
Ruby interactive shell (irb.exe)

C:\home\dh0072\rq>irb

# load a local file b.py for test

irb(main):001:0> load "b.rb"
=> true

# instantiate an object and call a method

irb(main):002:0> x = B.new
=> #
irb(main):003:0> x.p
B
=> nil

# edit b.py offline to change method ''p'' to do something different then
reload

irb(main):004:0> load "b.rb"
=> true
irb(main):005:0> x.p
BBBB
=> nil

# binding of variable ''x'' IS changed. points to new version

irb(main):006:0> y = B.new
=> #
irb(main):007:0> y.p
BBBB
=> nil

# new instance of ''b'' points to new version



dmh2000启发我们:
dmh2000 enlightened us with:
当您想要更改某些内容时,您可以在环境外编辑相同的源文件,并在
交互式环境中重新加载它们。但是,不同之处在于:使用Python,当你重新加载源文件(用Python术语表示模块)时,你的现有变量似乎与旧版本的实现保持联系似乎是
该模块。


IMO这是唯一正确的做事方式。变量指向内存中的
。当一个模块被重新加载时,内存中的那个东西仍然存在 - 因为它被引用,它不会被垃圾收集

收集。

Ruby交互式shell(irb.exe)

C:\ home \dh0072 \ rq> irb

#加载本地文件b.py进行测试

irb(主要):001:0>加载b.rb
=>真实的
#实例化一个对象并调用一个方法

irb(main):002:0> x = B.new
=> #
irb(主要):003:0> x.p
B
=>没有

#edit b.py离线改变方法''p''做一些不同的事情然后重新加载

irb(主要):004:0>加载b.rb
=> true
irb(main):005:0> x.p
BBBB
=> nil
When you want to change something, you can edit those same source
files outside the environment and reload them from within the
interactive environment. But, here is the difference: with Python,
when you reload the source file (module in Python terms), it seems
that your existing variables stay bound to the implementations from
the old version of the module.
IMO this is the only proper way of doing things. A variable points to
something in memory. When a module is reloaded, that thing in memory
is still there - because it is referenced, it does not get garbage
collected.
Ruby interactive shell (irb.exe)

C:\home\dh0072\rq>irb

# load a local file b.py for test

irb(main):001:0> load "b.rb"
=> true

# instantiate an object and call a method

irb(main):002:0> x = B.new
=> #
irb(main):003:0> x.p
B
=> nil

# edit b.py offline to change method ''p'' to do something different then
reload

irb(main):004:0> load "b.rb"
=> true
irb(main):005:0> x.p
BBBB
=> nil

# binding of variable ''x'' IS changed. points to new version




恕我直言,这是令人讨厌的。已经执行了一项任务。

重新加载某些模块不应该(再次,恕我直言)在重新加载之前更改

发生的任何事情。


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



IMHO this is nasty. An assignment has already been performed.
Reloading some module should not (again, IMHO) change anything that
happened before it was reloaded.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


>在这两种语言中,你可以启动交互式解释器
> In both languages, you can start up the interactive interpreter
(''python''和''irb''),加载源文件并做一些事情,比如创建
对象并调用它们方法。当您想要更改某些内容时,您可以在环境之外编辑相同的源文件,并在交互式环境中重新加载它们。但是,这里有不同之处:使用Python,当你重新加载源文件(用Python术语中的模块)时,你的现有变量似乎仍然与旧版本的
实现绑定该模块。在Ruby中,在重新加载之后,您现有的变量指向新的实现。至少,这就是类和它们的方法所发生的事情。这意味着在Python中,如果你有一个交互式构建的应用程序,对对象的引用,指向对象的数据结构等,你将需要重建该应用程序才能获得新的
实施。使用Ruby,当你加载新的实现时,你立即得到它。
(''python'' and ''irb''), load source files and do stuff, like create
objects and call their methods. When you want to change something, you
can edit those same source files outside the environment and reload
them from within the interactive environment. But, here is the
difference: with Python, when you reload the source file (module in
Python terms), it seems that your existing variables stay bound to the
implementations from the old version of the module. In Ruby, after a
reload, your existing variables point to the new implementation. At
least, that is what happens with classes and their methods. This means
that in Python, if you have an application built up interactively, with
references to objects, data structures pointing to objects, etc., you
would have to reconstruct that application to get the new
implementation. With Ruby, when you load the new implementation, you
get it immediately.




我不知道ruby足以评论_how_他们实现了。但是我知道

python足以告诉你你的观察结果是正确的,并且完全符合
与python通常所做的一致。


这里的问题源于这样的事实:在python中,有名称和

值。后者(也称为对象)可以绑定到名称。


现在加载模块会创建类对象,并以名称绑定它们 -

类名。但这个名字并不特别。你可以,例如确认


等级Foo:

通过


Foo = 10


这将创建一个绑定到名称Foo的类 - 并且重新绑定

非常命名为完全不同的东西(tm)。


现在创建一个该类的实例创建一个对象,它的类有一个

_reference_ - 而不是它的名字!


这意味着没有连接到那个班的_name_!好吧,

可以达到这个目的,因为该类知道它被创建时绑定的名称 - 但这仅用于纪录目的。


重新加载模块时,会创建一个新的类对象。并且绑定到

名称与之前的名称相同 - 但是之前创建的所有实例都保留了对旧类的

引用。这很有道理:在python中,每个实例都可以单独创建类,如果想要通过元类或者像这样的工厂函数来获得



def createClass():

class Foo:

pass

return Foo


所以 - 如果有效的话,人们怎么也知道这个重新加载

a模块有什么特别之处。


此致,


Diez



I don''t know ruby enough to comment on _how_ they achieve that. But I know
python enough to tell you that your observation is correct, and perfectly
consistent with what python ususally does.

The problem here stems from the fact that in python, there are names and
values. The latter (also referred to as objects) can be bound to a name.

Now loading a module creates class-objects, and binds them under a name -
the class-name. But this name is by no means special. You can e.g. do

class Foo:
pass

Foo = 10

That will create a class that is bound to the name Foo - and the rebind that
very name to something completely different(tm).

Now creating an instance of that class creates an object, that has a
_reference_ to its class - not its name!

Which means that there is no connection to the _name_ of that class! Well,
one can get to that, because the class knows the name it was bound to when
it was created - but that is only for documentary purposes.

When reloading a module, a new class object is created. And bound to the
name same name as before - but all instances created before keep a
reference to their old class. Which makes perfect sense: in python, it is
possible to create classes individually for each instance, if one wants to
- by metaclasses, or factoryfunctions like this:

def createClass():
class Foo:
pass
return Foo

So - if that works, there is no way how one can know that this reloading of
a module is anything special.

Regards,

Diez


这篇关于Python和Ruby交互式shell的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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