Python 中的一切都是像 Ruby 一样的对象吗? [英] Is everything an object in Python like Ruby?

查看:52
本文介绍了Python 中的一切都是像 Ruby 一样的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个 Stack Overflow 问题上读到 Python 就像 Ruby,因为它与一切都是对象"有关.Python 中的一切都是对象,就像 Ruby 一样.

I read on another Stack Overflow question that Python was just like Ruby, as it relates to "everything's an object," and everything in Python was an object, just like Ruby.

这是真的吗?Python 中的一切都是像 Ruby 一样的对象吗?

Is this true? Is everything an object in Python like Ruby?

两者在这方面有何不同,还是真的相同?例如,你能不能拿一个数字来做我见过的 Ruby 的事情:

How are the two different in this respect or are they really the same? For example, can you take a number and do the Ruby stuff I've seen like:

y = 5.plus 6

可以在 Python 中以同样的方式完成吗?

Can that be done the same way in Python?

推荐答案

DiveIntoPython - 一切皆对象

Python 中的一切都是对象,几乎所有的东西都有属性和方法.所有函数都有一个内置属性__doc__,它返回函数源代码中定义的doc string.sys 模块是一个对象,它具有(除其他外)一个名为 path 的属性.等等.

Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function's source code. The sys module is an object which has (among other things) an attribute called path. And so forth.

不过,这引出了一个问题.什么是对象?不同的编程语言以不同的方式定义对象".在某些情况下,这意味着所有对象必须具有属性和方法;在其他情况下,这意味着所有对象都是可子类的.在 Python 中,定义更宽松;一些对象既没有属性也没有方法(更多内容在第 3 章),并且并非所有对象都是可子类的(更多内容在第 5 章).但一切都是对象,因为它可以分配给变量或作为参数传递给函数(第 4 章将详细介绍).

Still, this begs the question. What is an object? Different programming languages define "object" in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).

Ruby 文档 -从 Python 到 Ruby

与 Python 一样,在 Ruby 中,...一切都是对象

As with Python, in Ruby,... Everything is an object

所以你可以从 Ruby 自己的网站获得它:在 Python 中,一切都是对象.

So there you have it from Ruby's own website: in Python everything is an object.

这篇关于Python 中的一切都是像 Ruby 一样的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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