references/pointers in Python?

Anton Lavrik asl at tepkom.ru
Wed Sep 12 13:09:28 EDT 2001


On Wed, Sep 12, 2001 at 12:28:31PM -0400, Ignacio Vazquez-Abrams wrote:
> On Wed, 12 Sep 2001, Anton Lavrik wrote:
> 
> > Generally, I want to define a referce to an integer, or to the
> > identifier, that holds it.
> > Python is full of references, is it possible to define it directly? Or
> > maybe there are some different ways to do it?
> 
> It's true that Python is full of references, but those are references to
> instances, not types.
> 
> You can do something like the following:
> 
> ---
> class MyVal:
>   def __init__(self, val):
>     self.set(val)
>   def set(self, val):
>     self.value=val
>   def __call__(self):
>     return self.value
> 
> a=MyVal(5)
> b=a
> b.set(3)
> print a()
> ---

Is this the only way? Actualy, I expect some perfomace when operating
with integers. And that would be a stupid work of overloading every integer
operation for the class.
Anyway, is it hard to provide such direct mechanism for immutable
instances? As I understand that how it operates now:

a = 10 (now a references to 10)
b = a ( now b references to the same value(instance) 10)
b = 20 ( now b doesn't references to 10, it references to another
value 20. And we decrement reference counter of the value(instance) 10)

So, IMHO, it's not so hard to implement Perl-like references here.

And does this all mean that I should move on to Perl or any other
language, if I need effective implementation of references :) ?


-- 
Anton.





More information about the Python-list mailing list