-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())
5
  • Where is Digit defined? Commented Dec 28, 2021 at 23:29
  • @Samwise There was a formatting typo that OP has now fixed Commented Dec 28, 2021 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? Commented Dec 28, 2021 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 How to Ask. Commented Dec 28, 2021 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 Commented Dec 29, 2021 at 0:12

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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'

Comments

-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.

Comments

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.