Python dumps core with this.. how come?

Fredrik Lundh fredrik at pythonware.com
Sat Jan 22 03:50:58 EST 2000


Roey Katz <katz at glue.umd.edu> wrote:
> When I import this module, the interpreter segfaults. 
>
> Thiis module provides a little guard mechanism to keep track of
> changes to registered variables. The error occurs when you ty to
> instantiate class 'basic_monitor'.

looks like a recursive call in getattr (if "res" is
not already available in the instance, it asks
__getattr__ to look it up).

make sure to define "res" in the __init__ function,
and everything will work as you expected.

> Also, I have another question (very old, and I can't find this in the
> FAQ):  I want to explicitly take a reference to an integer.

1. reset your brain ;-)
2. variables are named references, not small boxes
   that hold data values
3. *all* variables in python are references.  there
   is no other thing.
4. integers cannot be modified in place.

in other words, Python always copies references, but
you cannot modify objects in place unless their inter-
face allows you to (numbers, tuples and strings cannot
be modified in place, while lists and dictionaries can --
the only difference is that the former doesn't provide
any methods that let you modify them...)

> is there some sort of rule-list to follow to figure out when
> exactly Python will take a reference (like assigning from an
> object) or a literal value (int's)?

yes, and it's very simple:

Q: when does Python copy a reference and not a value?
A: always!

related FAQ entries:
http://www.python.org/doc/FAQ.html#4.35
http://www.python.org/doc/FAQ.html#4.38
http://www.python.org/doc/FAQ.html#4.50
http://www.python.org/doc/FAQ.html#4.89
and probably a few more.

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
— see it live on the python conference! &smiley; -->





More information about the Python-list mailing list