views:

589

answers:

7

I did google. I may not have searched right. I read on another Stack Overflow question/comment that Python was just like Ruby, as it relates to "everything's and object," and everything in Python was an object, just like Ruby.

Is this true? Is everything an object in python like ruby?

Thank you.

EDIT:

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

Can that be done the same way in Python?

+15  A: 

http://www.diveintopython.org/getting_to_know_python/everything_is_an_object.html

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.

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).

http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/

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

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

Unknown
+1 well put. As a Rubyist, I tend towards the "objects must have methods" side of the argument, but the "everything can be passed as an argument" argument is well put (and I suspect better reflects the Pythonista viewpoint)
rampion
"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)." That doesn't make any sense to me. Wouldn't that make Java int objects?
James McMahon
+1  A: 

Yep, as far as I know everything is an object in Python. Certainly the primitive and builtin types (int, long, str, float, etc.) can be subclassed - and in fact the types themselves are objects. Functions are objects, classes are objects, even code blocks are objects in a sense... I can't think of anything in Python that can't be treated as an object.

David Zaslavsky
A: 

Python's documentation:

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects.

This isn't quite true, for performance reasons there are some types, that are primitive values. (Ints, floats…). This limitation isn't quite obvious as those primitive types look like classes, but don't behave exactly like them.

Georg
No, even ints and floats are objects in Python. Try dir(1)
RichieHindle
The way ruby handles this -- I suspect Python is the same -- is to treat "primitive" types as real primitive values, then to convert them to objects transparently when necessary (ie. when a method is called on them)
Burke
Not really -- an int for example "is an object" just like any other (it can be passed around as a PyObject* in the code of the popular CPython implementation, etc, etc), no "conversion" required or desired.
Alex Martelli
@Burke: Actually, CPython (the official implementation of Python) does not do this. It creates a wrapper object for every single type. However, both the Psyco library and the PyPy project address this.
musicfreak
+9  A: 

In answer to your second question, yes:

>>> (1).__add__(2)
3
RichieHindle
Yep. Or even `1. __add__(2)` (note the space). Will even work for floats: `1..__add__(2)`.
dF
+6  A: 

"everything" is a tad of an overbid, for both Python and Ruby -- for example, if is not "an object", rather it's a keyword used to start a conditional statement or (in Python) inside list comprehensions and generator expressions. The enthusiasm of finding out that functions, classes, methods, and all sort of such things that aren't really objects in (say) C++, are objects in Ruby or Python, causes such enthusiasm. Other things may be objects in Ruby but not Python or viceversa (code blocks, regular expressions, ...).

Alex Martelli
Yes, but even on smalltalk, where 'if' is a method of block (Proc/lambda on ruby, lambdas on python, closures for language-agnostic guys), and everything is an object, has its keywords true, false, nil, self, super and thisContext. Sure enough, those are more like pseudo-variables...
Daniel Ribeiro
+13  A: 

While everything is an object in Python, it differs from Ruby in its approach to resolving names and interacting with objects.

For example, while Ruby provides you with a 'to_s' method on the Object base class, in order to expose that functionality, Python integrates it into the string type itself - you convert a type to a string by constructing a string from it. Instead of 5.to_s, you have str(5).

Don't be fooled, though. There's still a method behind the scenes - which is why this code works:

(5).__str__()

So in practice, the two are fundamentally similar, but you use them differently. Length for sequences like lists and tuples in Python is another example of this principle at work - the actual feature is built upon methods with special names, but exposed through a simpler, easier-to-use interface (the len function).

The python equivalent to what you wrote in your question would thus be:

(5).__add__(6)

The other difference that's important is how global functions are implemented. In python, globals are represented by a dictionary (as are locals). This means that the following:

foo(5)

Is equivalent to this in python:

globals()["foo"].__call__(5)

While ruby effectively does this:

Object.foo(5)

This has a large impact on the approach used when writing code in both languages. Ruby libraries tend to grow through the addition of methods to existing types like Object, while Python libraries tend to grow through the addition of global functions to a given module.

Kevin Gadd
"Python libraries tend to grow through the addition of global functions to a given module." This is not really correct. globals()["foo"] returns an object. The __call__ is a method, not a global function. You can't do globals()["__call__"]
Unknown
My point was that foo is not a method of the module. It's an attribute of the module, which happens to be callable, and importing it also adds it to globals(). __call__ is a method, yes.In Ruby, foo is a method of Object, in comparison.
Kevin Gadd
A: 

To add a comment to other people's excellent answers: everything is an object, but some – notably strings and numeric types – are immutable. This means that these types behave the way they do in languages like C or Java (where integers, etc. are not objects) with respect to assignment, parameter passing, etc, and you never have to worry about traps caused by pass-by-reference. It's rather a good solution :-)

John Fouhy