Python Forum
How I can create reference to member of the class instance?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How I can create reference to member of the class instance?
#1
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?
Reply
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
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.
Reply
#7
Gribouillis, thank you!
Reply
#8
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
Reply
#9
nilamo, thank you! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 316 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Class member become static Quasar999 1 680 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,895 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,521 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,920 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  Access instance of a class Pavel_47 5 2,089 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,874 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Class Instance angus1964 4 2,448 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Create Generator in the Class quest 1 2,133 Apr-15-2021, 03:30 PM
Last Post: jefsummers
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,648 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020