5

I am learning a tutorial on python.It is explaining how functions are first class objects in Python.

def foo():
        pass
print(foo.__class__)

print(issubclass(foo.__class__,object))

The output that I get for the above code is

<type 'function'>
True

This program is supposed to demonstrate that functions are first class objects in python? My questions are as follows.

  1. How does the above code prove that functions are fist class objects?
  2. What are the attributes of a first class object?
  3. what does function.__class__ signify? It returns a tuple <type,function> which doesn't mean much?
Wolph
  • 78,177
  • 11
  • 137
  • 148
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 3
    The fact that it __has__ a class makes it a fist[SIC] class object – Wolph Apr 12 '14 at 23:53
  • so does everything in python have __class__ member inside it? and the output signifies that it is a object of type function? – liv2hak Apr 12 '14 at 23:56

5 Answers5

12

Here's what Guido says about first class objects in his blog:

One of my goals for Python was to make it so that all objects were "first class." By this, I meant that I wanted all objects that could be named in the language (e.g., integers, strings, functions, classes, modules, methods, etc.) to have equal status. That is, they can be assigned to variables, placed in lists, stored in dictionaries, passed as arguments, and so forth.

The whole blog post is worth reading.

In the example you posted, the tutorial may be making the point that first class objects are generally descendents of the "object" class.

Tom Barron
  • 1,554
  • 18
  • 23
7

First-class simply means that functions can be treated as a value -- that is you can assign them to variables, return them from functions, as well as pass them in as a parameter. That is you can do code like:

>>> def say_hi():
        print "hi"
>>> def say_bye():
        print "bye"
>>> f = say_hi
>>> f()
hi
>>> f = say_bye
>>> f()
bye

This is useful as you can now assign functions to variables like any ordinary variable:

>>> for f in (say_hi, say_bye):
        f()
hi
bye

Or write higher order functions (that take functions as parameters):

>>> def call_func_n_times(f, n):
        for i in range(n):
            f()
>>> call_func_n_times(say_hi, 3)
hi
hi
hi
>>> call_func_n_times(say_bye, 2)
bye
bye

About __class__ in python tells what type of object you have. E.g., if you define an list object in python: a = [1,2,3], then a.__class__ will be <type 'list'>. If you have a datetime (from datetime import datetime and then d = datetime.now(), then the type of d instance will be <type 'datetime.datetime'>. They were just showing that in python a function is not a brand new concept. It's just an ordinary object of <type 'function'>.

dr jimbob
  • 17,259
  • 7
  • 59
  • 81
2

You proved that functions are first class objects because you were allowed to pass foo as an argument to a method.

The attributes of first class objects was nicely summarised in this post: https://stackoverflow.com/a/245208/3248346

Depending on the language, this can imply:

  • being expressible as an anonymous literal value
  • being storable in variables
  • being storable in data structures
  • having an intrinsic identity (independent of any given name)
  • being comparable for equality with other entities
  • being passable as a parameter to a procedure/function
  • being returnable as the result of a procedure/function
  • being constructible at runtime
  • being printable
  • being readable
  • being transmissible among distributed processes
  • being storable outside running processes
Community
  • 1
  • 1
  • which method are we talking about? print ? we pass foo.__class__ to print,not foo itself. As I understand foo.__class__ is an attribute (or member variable) of foo? – liv2hak Apr 12 '14 at 23:58
2

Regarding your third question, <type 'function'> isn't a tuple. Python's tuple notation is (a,b), not angle brackets.

foo.__class__ returns a class object, that is, an object which represents the class to which foo belongs; class objects happen to produce descriptive strings in the interpreter, in this case telling you that the class of foo is the type called 'function'. (Classes and types are basically the same in modern Python.)

It doesn't mean a whole lot other than that, like any other object, functions have a type:

>>> x = 1
>>> x.__class__
<type 'int'>

>>> y = "bar"
>>> y.__class__
<type 'str'>

>>> def foo(): pass
... 
>>> foo.__class__
<type 'function'>
Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
1

Regarding your comment to @I.K.s answer, f_at_2() in the following would be the method.

def f_at_2(f):
    return f(2)

def foo(n):
    return n ** n

def bar(n):
    return n * n

def baz(n):
    return n / 2

funcs = [foo, bar, baz]

for f in funcs:
    print f.func_name, f_at_2(f)

...

>>> 
foo 4
bar 4
baz 1
>>> 

A method is a function of/in a class, but the concept also applies to a function (outside of a class). The functions (as objects) are contained in a data structure and passed to another object.

wwii
  • 23,232
  • 7
  • 37
  • 77