Dec-04-2018, 04:52 PM
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 = 1234I 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