0

So I've got a class in python representing an epidemic simulation. So in many cases I have an 'S', 'I' and possibly 'R' states (susceptible, infected, recovered). So I've got something crudely like this method in the class:

def S(self):
    return data['S']

So I can access the data with foo.S but maybe I'm looking at some other simulation where I've got other statuses (let's say 'E' is also a status (exposed)). I'd like to be able to automatically get the other data by a similar method: foo.E. I want to be able to do any particular collection of statuses without having to change the class itself.

So I'm looking for a way to modify __init__ for MyClass so that I can define

foo = MyClass(status_list = ('S', 'I', 'E', 'R', 'someotherstatus'))

and then I can automatically access foo.someotherstatus. With my current understanding, the only way I can do this is to go into the code of MyClass and explicitly define a method someotherstatus which will return data['someotherstatus'].

3
  • Just my two cents: I usually don't like accessing values in dict directly by keys, but if that's the only thing you want to do why creating those simple getters instead of accessing them directly with foo.data['S'] ? Commented Aug 23, 2019 at 6:42
  • do you want it to be a function or property? You defined S as function. That means that you can access the value via foo.S() not foo.S Commented Aug 23, 2019 at 6:43
  • @Raphael In my case I'm interested in both options. I could imagine wanting to have foo.S(bar) to get information about those S individuals which satisfies some other property. Commented Aug 23, 2019 at 6:45

2 Answers 2

2

IIUC, try using setattr:

import pandas as pd

data = pd.DataFrame(columns = ['S', 'I', 'E', 'R', 'someotherstatus'])

class MyClass:
    def __init__(self, status_list):
        for i in status_list:
            setattr(self, i, data[i])

foo = MyClass(status_list = ('S', 'I', 'E', 'R', 'someotherstatus'))
foo.S

Output:

Series([], Name: S, dtype: object)
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like what I'm after. I'll play with it to be sure.
0

Maybe this gives you some ideas:

class MyClass:

    def get_status(self, status, other_param=None):
        if other_param:
            return self.data[status] + other_param
        else:
            return self.data[status]

    def __init__(self):
        self.data = {
            'S': 1,
            'someotherstatus': 2,
        }

        # without other params
        setattr(self, 'someotherstatus', self.get_status('someotherstatus'))

        # with an other param
        setattr(self, 'someotherstatus_with_param', lambda other_param: self.get_status('someotherstatus', other_param))

obj = MyClass()
print(obj.someotherstatus)  # 2
print(obj.someotherstatus_with_param(2)) # 4

1 Comment

What's the point of those setattr calls? They are the same as just doing self.someotherstatus = self.get_status(...). The purpose of setattr is to either handle dynamic attribute names only known at runtime or to pass it around in expressions...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.