Python dumps core with this.. how come?

Roey Katz katz at glue.umd.edu
Sat Jan 22 10:16:20 EST 2000


On Sat, 22 Jan 2000 09:50:58 +0100, "Fredrik Lundh"
<fredrik at pythonware.com> wrote:

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

Thanks, I got it right after I posted :)
this brings up another concern. Given
the following definition:

   class A:

      def __getattr__( self, name ):
        return -1    
      def __setattr__( self, name, val ):
         self.__dict__[ name ] = val

I see that self.__dict__ must be a special variable
in that setting it somehow avoids a recursive call 
to __setattr__()? 



>> 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.

Ok, OK:  this is what I always get when I ask this question,
but then why does this happen:

     >>> a = 3 
     >>> b = a 
     >>> b
     3 
     >>> a = 5
     >>> b
     3

I mean, what happens if I have a complicated path 
to a variable and I want to alias it:

    alias = long_name.hard_to_type[ 1 ].integer_value

    # arbitrary calculations that would be 
    # torturous with any label longer than 'alias'
    if alias > someValue:
       result = (alias * 5 ) / (alias + 4 )
    else:
       result = (alias * 3 ) / (alias + 2 )

>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...)

so where does this definition fit in the context I mentioned?
I understand that if I have a function and pass to it a parameter,
I can modify it in-place (although that I cannot modify numbers,
strings or tuples in-place seems to me inconsistent).  
So for example, these are roughly equivalent:
 
    void setValue(  int& x ) { x = 3  }    // C++
    void setValue(  int *x  ) { *x = 3 }    // C
    def  setValue( x ):    x = 3             # Python

whoops, we cannot modfiy integers in-place, how invonvenient.  Though
we could always wrap them in a class and overload its = operator
(wait! no = operator to overload! double inconvenience!) or have a
specially defined set() function:

    class A:
       def set( self, val ):
            self.value = val

    def setValue( a ):
       a.setValue( 5 ) 

so that works, but do you see how it is sort of polluting? I mean, the
interface is not so simple anymore. 

>Q: when does Python copy a reference and not a value?
>A: always!
ayeee! missed it! terribly sorry :)


ok, here's a third question, and it builds on the above inquiry
regarding a lack of an assignment operator.  What happens when I want
to do convert this for loop:

   for i,j in dest, src:
        dest[ i ] = src[ j ]

into a fast map() expression:

   map( dest.assign_operator, src )

like I mentioned -- there is no assignment operator to overload! 
the trouble here is that there may be more situations in which 
I cannot use  filter() to narrow down the list to only the elements
that I want and then copy it into another list with src.copy().  

Thanks!
Roey



More information about the Python-list mailing list