Python Forum
How I can create reference to member of the class instance? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How I can create reference to member of the class instance? (/thread-14512.html)



How I can create reference to member of the class instance? - AlekseyPython - Dec-04-2018

Python 3.7.1

I wrote this class:
class Cycler(object):
    def __init__(self, data):
        self.data = data

    def do(self):
        My_ref = self.data
        My_ref = 2
And now I want create small name to simplify the code. In above code the instance self does not change the value of the variable data (but variable My_ref will now refer to an object of type 'int' with a value of 2).

How I can create writable reference to a member of the class instance?


RE: How I can create reference to member of the class instance? - buran - Dec-04-2018

It's not quite clear what you want to achieve. As is My_ref from your code is not visible/accessible outside Cycler.do() method.
Let's assume your Cycler code does not have do method at all. You can do follwoing
class Cycler(object):
    def __init__(self, data):
        self.data = data
        
# create instance of class Cycler        
cycler = Cycler(2)

#access instance property data
print(cycler.data)

#change instance property data
cycler.data = 5

#access instance property data again
print(cycler.data)



RE: How I can create reference to member of the class instance? - AlekseyPython - Dec-04-2018

buran, thank you!
I understand how to use members of the class instance in client code. I want to learn how to create a link for quick access to the members of a class instance INSIDE a class definition.


RE: How I can create reference to member of the class instance? - buran - Dec-04-2018

Still not sure if I understand, but is it this
class Cycler(object):
    def __init__(self, data):
        self.data = data
        
    def increase(self, value):
        self.data += value
        
# create instance of class Cycler        
cycler = Cycler(2)

#access instance property data
print(cycler.data)

#change instance property data
cycler.increase(3)

#access instance property data again
print(cycler.data)
You access members (properties and methods) using self. self being used to reference the instance is just a convention.


RE: How I can create reference to member of the class instance? - AlekseyPython - Dec-04-2018

buran, I know how to use class instance in client code. I want create class with simple code, and for this I want to create short names of variables in the class methods. For example if in class method I write:
ref = self.data
,then in the code of this method below, use simple ref instead of self.data, including for writing a new value.


RE: How I can create reference to member of the class instance? - Gribouillis - Dec-04-2018

What you want is not possible because one cannot overload or override the assignment operator in python. The semantics of
ref = 2
is to update the locals dictionary (or the globals dictionary) to have the key 'ref' point to the value 2. Note that the meaning of the statement is purely symbolic. The word 'ref' is used but not any operation that you have done before with it. That's why Python's documentation often uses 'name' instead of 'variable'. A variable in python is nothing but a word.

You could have either another attribute assignment or a function call to mean set self.data to value 2 because attribute assignment can be overriden
r.f = 2
ref(2)
However this will slow down the execution because direct attribute access is fast.


RE: How I can create reference to member of the class instance? - AlekseyPython - Dec-04-2018

Gribouillis, thank you!


RE: How I can create reference to member of the class instance? - nilamo - Dec-04-2018

You could use a smaller variable name for the instance. Instead of self, it could be s. If less typing is your goal:
class Foo:
    def do(s):
        s.data = 1234
I have a feeling that's not what you're looking for, though. It sounds like you're looking for a mutable data structure, such as a list:
>>> class Foo:
...   def __init__(self):
...     self.data = []
...   def do(self):
...     ref = self.data
...     ref.append(4)
...     return ref
...
>>> x = Foo()
>>> ref = x.do()
>>> ref
[4]
>>> ref[0] = "the cow goes 'SHAZOOO'"
>>> x.do()
["the cow goes 'SHAZOOO'", 4]
If that's what you're looking for, and using a list/dict won't work, you could build your own mutable structure that behaves similarly. You can't override the assignment operator, but you can override most others. Something like this (though, if you do this, anyone else who looks at your code will probably hate you lol):
>>> class Atom:
...   def __init__(self, value):
...     self.value = value
...   def __or__(self, other):
...     self.value = other
...     return self
...   def __repr__(self):
...     return str(self.value)
...
>>> class Spam:
...   def __init__(self, value):
...     self.value = Atom(value)
...   def do(self):
...     ref = self.value
...     print(f"original value: {ref}")
...     ref |= "moo"
...     return ref
...
>>> x = Spam("cat")
>>> y = x.do()
original value: cat
>>> y
moo
>>> y |= "fishy"
>>> y
fishy
>>> x.do()
original value: fishy
moo



RE: How I can create reference to member of the class instance? - AlekseyPython - Dec-05-2018

nilamo, thank you! :)