Is there a reference/alias/pointer in Python?

James Henderson james at logicalprogression.net
Thu Jul 22 21:56:40 EDT 2004


Daniel Eloff wrote:

> Maybe I’m still thinking in C++, but see if this makes sense to you.
> 
>  
> 
> I have a UserOptionsClass that contains program options. It has a 
> member, Users, that is a dictionary. I defined __getitem__ so that I can 
> access a users options via OptionsInstance[ ‘username’ ]. Now I defined 
> a second member, CurrentUser which is set to the Users [ 
> ‘current_users_username ‘ ]. I understand it’s ref counted, so that 
> doesn’t make a new copy, but doesn’t that also mean that if I change it, 
> it will be made into a new copy separate from Users [ 
> ‘current_users_username’ ] ? Is there any way I can change that behaviour?
> 
>  
> 
> Thanks,
> 
> -Dan
> 

Hi Dan,

Couldn't you just make CurrentUser a method that returns 
self.Users['current_users_username']?

If you really want CurrentUser to appear as an attribute (data member) 
you could make it a descriptor.  Something like:

class UserOptionsClass:
     def __init__(self, currentUser):
         self.Users = {'current_users_username': currentUser}
     def getCurrentUser(self):
          return self.Users['current_users_username']
     def setCurrentUser(self, name):
          self.Users['current_users_username'] = name
     CurrentUser = property(getCurrentUser, setCurrentUser)

HTH,
James




More information about the Python-list mailing list