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?
#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


Messages In This Thread
RE: How I can create reference to member of the class instance? - by nilamo - Dec-04-2018, 04:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 690 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  Class member become static Quasar999 1 765 Sep-16-2023, 12:52 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,197 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,776 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 2,009 Dec-15-2021, 01:53 PM
Last Post: deanhystad
  Access instance of a class Pavel_47 5 2,210 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 5,053 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Class Instance angus1964 4 2,555 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Create Generator in the Class quest 1 2,203 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,770 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