3

This query is in continuation with link to understand further on this point:

In the case of functions, you have an object which has certain fields, which contain e. g. the code in terms of bytecode, the number of parameters it has, etc.

My question:

1) How do i visualise a function being represented as an object?(NPE answered this question here)

2) How do i visualise an higher order function being represented as an object?

3) How do i visualise modules being represented as an object? say 'import operator'

4) Are operators like '+' '>' '!=' '==' '=' are also mapped to some object methods? say for expr 'check = 2 < 3', Does this internally call some method of type(2) or type(3) to evaluate '<' operator?

Community
  • 1
  • 1
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • glglgl -- Seperate query is raised as requested – overexchange Mar 28 '14 at 06:44
  • 3
    I'm assuming you're trying to continue on from some other question. Each question on Stack Overflow is supposed to be self contained. You should make this question legible and easy to understand without having to visit any links. As it stands it is very difficult to tell what your actual question is. – Asad Saeeduddin Mar 28 '14 at 06:45
  • Everything we process in python is `object`. – Nishant Nawarkhede Mar 28 '14 at 07:21
  • @Sham You failed to explain exactly what is unclear about that. I as well thought it was something completely else what you were asking... – glglgl Mar 28 '14 at 07:59
  • @g.d.d.c i modified the query now, can u please re-open this question? – overexchange Mar 29 '14 at 02:11

1 Answers1

6

All this is saying is that, in Python, functions are objects like any other.

For example:

In [5]: def f(): pass

Now f is an object of type function:

In [6]: type(f)
Out[6]: function

If you examine it more closely, it contains a whole bunch of fields:

In [7]: dir(f)
Out[7]: 
['__call__',
 ...
 'func_closure',
 'func_code',
 'func_defaults',
 'func_dict',
 'func_doc',
 'func_globals',
 'func_name']

To pick one example, f.func_name is the function's name:

In [8]: f.func_name
Out[8]: 'f'

and f.func_code contains the code:

In [9]: f.func_code
Out[9]: <code object f at 0x11b5ad0, file "<ipython-input-5-87d1450e1c01>", line 1>

If you are really curious, you can drill down further:

In [10]: dir(f.func_code)
Out[10]: 
['__class__',
 ...
 'co_argcount',
 'co_cellvars',
 'co_code',
 'co_consts',
 'co_filename',
 'co_firstlineno',
 'co_flags',
 'co_freevars',
 'co_lnotab',
 'co_name',
 'co_names',
 'co_nlocals',
 'co_stacksize',
 'co_varnames']

and so on.

(The above output was produced using Python 2.7.3.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • >>> import sys >>> print(sys.version) 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] >>> def doNothing():pass >>> doNothing.func_name Traceback (most recent call last): File "", line 1, in doNothing.func_name AttributeError: 'function' object has no attribute 'func_name' >>> doNothing.func_code Traceback (most recent call last): File "", line 1, in doNothing.func_code AttributeError: 'function' object has no attribute 'func_code' – overexchange Mar 28 '14 at 06:56
  • 7
    @Sham: On Python 3, you'll want to use `__name__` and `__code__` instead of `func_name` and `func_code`. – user2357112 Mar 28 '14 at 06:58
  • @Sham You already saw that `dir(doNothing)` shows you what attributes you have. The ones which look like these you are looking for are probably the ones you really want. E. g., you look for `func_code` as it was named on 2.x, you see a `__code__` on 3.x --> you found what you llok for. – glglgl Mar 28 '14 at 08:01
  • @glglgl If it an higher order function, What attributes do i need t oreview to know the function passed into? – overexchange Mar 28 '14 at 09:15