7

I just checked out this very interesting mindmap:

http://www.mindmeister.com/10510492/python-underscore

And I was wondering what some of the new ones mean, like __code__ and __closure__. I googled around but nothing concrete. Does anyone know?

Ram Rachum
  • 77,567
  • 79
  • 223
  • 360

4 Answers4

7

From What's New in Python 3.0

The function attributes named func_X have been renamed to use the __X__ form, freeing up these names in the function attribute namespace for user-defined attributes. To wit, func_closure, func_code, func_defaults, func_dict, func_doc, func_globals, func_name were renamed to __closure__, __code__, __defaults__, __dict__, __doc__, __globals__, __name__, respectively.

Basically, same old Python 2 stuff, fancy new Python 3000 name.

You can learn more about most of these in PEP 232

Jack Lloyd
  • 8,066
  • 2
  • 35
  • 46
6

You actually have analogous fields in CPython 2.x:

>>> first = lambda x: lambda y: x
>>> f = first(2)
>>> type(f.func_code)
<type 'code'>
>>> map(type, f.func_closure)
[<type 'cell'>]

Edit: For more details on the meaning of these constructs please read about "user defined functions" and "code objects" explained in the Data Model chapter of the Python Reference.

Andrey Vlasovskikh
  • 16,003
  • 7
  • 41
  • 61
4

They used to be called

func_closure (now __closure__), func_code (now __code__)

(that should help googling).

A short explanation from here below.

  • func_closure: None or a tuple of cells that contain bindings for the function’s free variables (read-only)
  • func_code: The code object representing the compiled function body (writable)
ChristopheD
  • 106,997
  • 27
  • 158
  • 177
0

These are Python's special methods.

Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
Egon
  • 1,705
  • 18
  • 32