For different classes I have different settings (ex: addresses) for a shared object s. I have to reinforce the address variable s.addr every time the method is called since class ItemA and ItemB to to set s.addr to different addresses. Is there a way to simplify this redundancy?
Ex:
class ItemA:
def __init__(self,s,addr):
self.s = s
self.addr = addr
def a(self,addr):
self.s.addr=self.addr # anyway to remove this line that exist in every method
self.s.run_a()
def b(self,addr):
self.s.addr=self.addr #
self.s.run_b()
class ItemB:
def __init__(self,s,addr): # take same s as ItemA
self.s = s
self.addr = addr
def a(self,addr):
self.s.addr=self.addr #
self.s.run_a()
def c(self,addr):
self.s.addr=self.addr #
self.s.run_c()
ItemsA, B are called randomly so address needs to be update in every method.
A=ItemA(s,9) # object s, address 9
B=ItemB(s,7) # same object s, but use address 7
A.a()
B.a()
B.c()
A.b()
For extra info, it is to control instruments with GPIB and each of them has different address. s is pexepect that send generic command to the address specified in s.addr . But interested in more general python question.
You have three classes:
ItemA
,ItemB
, and whatever class generatesS
.S
objects are "shared" between A and B objects.S
has an identifier used by each class, but the value is different between A and B. You refer to both identifiers as "address". You want to know how to maintain "the" address, such that each of A and B accesses the address specific to that class.As I see it, the problem is trivial: these are two different attributes, but you are trying to stuff them into one. As you can see, this doesn't work. Instead, you need to make an attribute for each "address" -- just because A and B use the same external term for the feature, does not force you to use a single variable.
For instance:
Does that get you moving?