The "does Python have variables?" debate

Chris Angelico rosuav at gmail.com
Fri May 9 10:29:14 EDT 2014


On Sat, May 10, 2014 at 12:09 AM, Rustom Mody <rustompmody at gmail.com> wrote:
> Yes thats the point -- its a real valued spectrum, not a yes/no. eg.
>
> You write an app with Tkinter. Are you not using Tcl/Tk?

I'm not familiar enough with Tkinter to be sure, but I think you'd be
using Tk but not Tcl. There are a few leaks, though, so it may be
necessary to grok Tcl to use Tkinter (or at least, there may be times
when grokking Tcl will make it easier to debug Tkinter issues).

> You said earlier to Mark that the relation between python and C variables is irrelevant.  Lets take that as true.  Will it continue to be irrelevant when
> you need to write an extension in C?

The similarities and differences between the variable models are no
more relevant. What becomes relevant are the PyObject* pointer (the C
interface to a Python object (not variable)) and the various functions
for manipulating namespaces, mainly dictionaries. If anything, you
need to be more aware that Python and C variables are completely
different beasts. CPython's C API [1] is fairly manual; you INCREF and
DECREF objects, there's no concept of "automatic variables", etc etc
etc. For comparison, the V8 JavaScript engine tries to make it easy to
work with automatic variables - you create scope levels, and then a
whole lot of magic happens and things are really easy. At least,
they're easy so long as your code model perfectly follows what the
designers had in mind; if your code works in any other way, it becomes
a lot harder, as you end up either fighting against the scope system,
or just declaring a global scope object and using that everywhere. In
both cases, the relationship to C's variable model is coincidental at
best. The fact that you can go "x = 1;" in all three languages and
expect it to do the same thing is quite useless if you're trying to
write C++ code [2] that does "x = 1;" in Python and JavaScript.

ChrisA

[1] I'm not sure whether this is CPython's API or if it's part of the
Python language spec, as I've never written C extensions for any other
Python.
[2] The V8 API is actually a C++ API, not that the difference matters
to this discussion



More information about the Python-list mailing list