2

I keep hearing "everything is an object" in both Ruby and Python world. Well, what are built-in functions then? Can someone explain that in layperson English? For example:

file=open(abc.txt) 

open is a built-in function. Is it an object? Is it a method? Of what class?

How did we even end-up with functions in Python if everything is an object? I mean, shouldn't we be having class, objects, methods and attributes, instead of functions? I thought we had functions in languages like C. Python, Ruby and Java had classes, objects, attributes, methods.

In Ruby (irb), you could do something like 1.class and this will give you Fixnumit will show you which class it belongs to. I don't seem to be able to do this in Python shell. Is there an equivalent?

FYI:

  • I am using Python 2.7
  • relatively new to programming so please use layperson English. For instance, I read in one of the answers "But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function" --- no idea what that means.
2
  • 1
    How is "everything is an object" at conflict with having functions? Commented Dec 30, 2011 at 14:59
  • @delnan Coming from Java POV, there are no functions. There are methods. And each method belongs to some class. So, if you have a "function" that is an "object". Then that object must belong to some class. And the way to use it would be function.method(). Because you say "function" is an object in Python; I'd think, the way to use "open" would be open.method(). "open" being name of the object and "method" being the method/function. In Java, you can't have a method that doesn't belong to any class/object. Commented Dec 30, 2011 at 20:06

2 Answers 2

3

They are all listed here: http://docs.python.org/library/functions.html

The function open is an object (all functions are objects) and belongs to the __builtin__ module. They are simply built-in, and available to all objects, since they are automatically imported (something like from __builtin__ import *).

>>> print repr(open) <built-in function open> >>> print open.__module__ __builtin__ >>> import __builtin__ >>> print __builtin__.open <built-in function open> 

Update
You mentioned in your edit that you don't know what assigning a function means.

>>> o = open >>> print repr(open) <built-in function open> >>> print repr(o) <built-in function open> >>> o('file.txt') <open file 'file.txt', mode 'r' at 0x107fe49c0> 
Sign up to request clarification or add additional context in comments.

13 Comments

So this means builtin is a class? And open is a method of this class?
It's a module, not a class. And no, it's a function inside a module.
so, we dont just have classes, attributes, methods and objects. But we also have modules and functions in Python?
Modules are groups of classes, variables and functions. Classes are groups of methods (with arguments) and variables. Methods are functions that take the class state as the self argument. And everything is an object. And all of that must be very confusing if you're new to programming or OO.
@RedChar Everything in Python is an object, including modules, functions, classes and anything else that you see in the language.
|
0

Everything is an object, but not every object is an instance of a useful class in the classic sense. Some things you're better off treating as plain objects, as far as I can tell (functions are a good example that's already been given).

Note that you can use type(obj) or inspect obj.__class__ to replicate Ruby's .class method somewhat (see delnan's comment about the caveat for integers, though). You can also see the 'order' of the full inheritance sequence by issuing type.mro(type(obj)).

In [7]: type.mro(type(open)) Out[7]: [<type 'builtin_function_or_method'>, <type 'object'>] In [4]: import datetime In [5]: d = datetime.datetime(2009,11,11) In [6]: type.mro(type(d)) Out[6]: [<type 'datetime.datetime'>, <type 'datetime.date'>, <type 'object'>] 

1 Comment

Note though that 1.__class__ won't work, but only due to syntax. You need 1.0.__class__ or (1).__class___ to help Python realize it's not a floating point number but attribute access.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.