-1

I found code objects in Python. I'm curious as to what each of the variables in the constructors do. There is not much information on them in the builtin help function all I got was:

 class code(object)
 |  code(argcount, nlocals, stacksize, flags, codestring, constants, names,
 |        varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])
 |  
 |  Create a code object.  Not for the faint of heart.

That is obviously not very informative. What types do each of these inputs expect, and what do the values do? NOTE: I asked this question out of academic curiosity, and not for any specific coding purpose.

tox123
  • 318
  • 9
  • 21
  • 6
    It's not clear what you are asking, please try and rephrase your question, and include an example of what you want to happen, what you have tried, and how it doesn't work. – Gareth Latty Aug 01 '14 at 15:54
  • 2
    This actually looks pretty clear. The OP wants to learn how the constructor works for Python [code objects](https://docs.python.org/2/c-api/code.html), a sort of low level proto-function thing. There isn't much explanation for the 14 separate arguments, as this is very much an internal thing and the definition changes often. *Why* the OP wants to do this is less clear; it's not useful unless you're doing very low-level, Python-version-specific things. – user2357112 Aug 01 '14 at 16:23
  • thank you!!! I want to do it to better understand the python system, and I'm experimenting with making basic GUI's in tkinter. – tox123 Aug 01 '14 at 16:36
  • @x-x: you don't need to know anything at all about code objects in order to make a Tkinter GUI. – Bryan Oakley Aug 01 '14 at 20:37
  • @BryanOakley I found `compile(raw_input(">>>"),...` works within the GUI itself, and it was mostly just curiosity. – tox123 Aug 01 '14 at 22:15
  • @EdCottrel It's not a dup, I was referring to python 2. The difference can be seen in the arguments taken by the init. For instance, in python 2 there is no `kwonlyargcount` argument. – tox123 Aug 13 '16 at 01:04
  • @tox123 Fair enough. I've reopened it. – elixenide Aug 13 '16 at 01:06

1 Answers1

1

A Python code object is mostly just a container for its attributes. Each of the arguments you see for the constructor becomes an attribute with a co_ prefix (e.g. the argcount argument becomes the co_argcount attribute).

The constructor does do a bit of validation, so if the arguments are not of the right type, it will raise an exception right away (rather than only failing when the code object is used later on).

As for what the arguments and attributes mean, that's mostly documented in a big table in the documentation for the inspect module. Here's the relevant part:

code  co_argcount     number of arguments (not including * or ** args)   
      co_code         string of raw compiled bytecode    
      co_consts       tuple of constants used in the bytecode    
      co_filename     name of file in which this code object was created     
      co_firstlineno  number of first line in Python source code     
      co_flags        bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg   
      co_lnotab       encoded mapping of line numbers to bytecode indices    
      co_name         name with which this code object was defined   
      co_names        tuple of names of local variables      
      co_nlocals      number of local variables      
      co_stacksize    virtual machine stack space required   
      co_varnames     tuple of names of arguments and local variables

The attributes co_freevars and co_cellvars are not documented as far as I can see. They're related to closures, I think.

Blckknght
  • 100,903
  • 11
  • 120
  • 169