-1

I don't understand how to get the value of a field instance from python class. I added what I need this code to do in get_person method. The output should be 3 or None.

class Digit:
    def __init__(self, digit)
        self.digit = digit
    
    def get_person(self):
        # I want to get the value for the field 'Person' from digit, if 'Person' exists
        # else None
        
        
    
inst1 = Digit('Team=Hawkeye|Weapon=Bow|Person=3')
inst2 = Digit('Comics')

print(inst1.get_person(),
      inst2.get_person())
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Where is `Digit` defined? – Samwise Dec 28 '21 at 23:29
  • @Samwise There was a formatting typo that OP has now fixed – wjandrea Dec 28 '21 at 23:32
  • 2
    Passing a string like `'Team=Hawkeye|Weapon=Bow|Person=3'` when you know you will need to access these values is a poor design. Consider passing in a dictionary instead, then the problem goes away. Or parse those values out in the `__init__()` method so they are available. Also having a single property like `digit` that represents more than one kind of thing — sometimes a string like `"Comics"` sometimes key/value pairs — is a bad idea. What is it supposed to represent? – Mark Dec 28 '21 at 23:33
  • Welcome to Stack Overflow! Please take the [tour]. What is your question exactly? If you need help with implementing it, then what do you need help with exactly? What have you already tried? Please read [ask]. – wjandrea Dec 28 '21 at 23:34
  • @Mark 'Comics' is there to represent that it doesn't contain a value. So, when there is no value it returns None – user17625175 Dec 29 '21 at 00:12

3 Answers3

0

Your use case for the Digits class is possibly too broad given your two examples. Generally, it's best to have a class store the same types of data in each instance, for example Hawkeye, Bow, 3, and Thor, Hammer, 3. Adding Comics to inst2 suggests to me there is a better way of splitting up your classes, but without more context of what your code is actually doing it's hard to tell.

As was pointed out in the comments, using a string to store information like this is very messy and makes things hard to manage. A better way would be to use a dictionary, a way of storing names and values, for example, your team, weapon, and person values:

class Digit:
    def __init__(self, data):

        self.data = data
    
    def get_person(self):

        if "Person" in self.data.keys():
            return self.data["Person"]
        else:
            return None
    
inst1 = Digit({"Team":"Hawkeye", "Weapon":"Bow", "Person":3})
inst2 = Digit({"Comics":None})

print(inst1.get_person(),
      inst2.get_person())

A good starter guide for dictionaries can be found here

Christy Kail
  • 146
  • 7
0

Based on your example, there's no reason you shouldn't simply use dict:

inst1 = dict(Team='Hawkeye', Weapon='Bow', Person=3)
inst2 = dict()

print(inst1.get("Person"), inst2.get("Person"))  # prints '3 None'
Samwise
  • 68,105
  • 3
  • 30
  • 44
-2
class Digit: # here you define your class
    def __init__(self, digit):
        self.digit = digit # you initialize (set) your attribute.
    def get_person(self):
        return self.digit # you return the attribute

If the object doesn't exist, you won't be able to call its member get_person anyway.

wjandrea
  • 28,235
  • 9
  • 60
  • 81