-1

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.

jojokids
  • 27
  • 4
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. You are currently doing exactly that: storing the `addr` for later use ... except that you do not make use of it. I don't know what you mean by "I have to reinforrce": a value remains until you change it. – Prune Sep 24 '20 at 20:15
  • @prune, Thanks I try to clarify again by changing title and content, hopefully easier to understand. Would like to improve my codes. Did try research online ahead but must not have correct keywords. – jojokids Sep 25 '20 at 00:34

1 Answers1

0

You have three classes: ItemA, ItemB, and whatever class generates S. 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:

class ItemA:

    def __init__(self,s,addr):
        self.s = s
        self.s_addr = addr
    
    def a(self):
        self.s.run_a(self.s_addr)

Does that get you moving?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Your first paragraph is exactly what I am trying to say, thanks. Your code is working as that was my original way of writing it, by passing address as parameter. Both ways self.s_addr or self.s.addr=self.addr will be in every method a, b, c... right? was trying to find if it can be done only once. – jojokids Sep 25 '20 at 01:44
  • The data is not "in" the method: it's in the object. All you need is to have access to the `s` object with its two fields. – Prune Sep 25 '20 at 03:08