0

I am new to python and apologize in advance if this is too bad.

Suppose i dynamically make an object an attribute of another object. Can the assigned as an attribute object access the assigned to object's other attributes without inheritance or passing as an argument?

e.g:-

class human:
    def __init__(self):
        self.health = 100

class fire:
    def __init__(self):
        self.fire = 10
    def damage(self):
        ????.health -= self.fire   #can i do anything to get bill's health?

bill = human()
bill.fired = fire()
bill.fired.damage()   #the fired object wants to know the bill object's health

I know i can pass bill's health as an argument to the damage function:-

class human:
    def __init__(self):
        self.health = 100

class fire:
    def __init__(self):
        self.fire = 10
    def damage(self, obj):
        obj.health -= self.fire

bill = human()
bill.fired = fire()

print bill.health

bill.fired.damage(bill)   #now the fired object knows bill's health

print bill.health   #works fine

But is there any other way or is this a dead end? Apart from the inheritance. (I'm using python v2.7, but of course would like to know v3 solution too)

Once again i apologize if this question is too bad or has been answered. I tried to read this one Can an attribute access another attribute?, but i couldn't understand it, its too complex. And if i google this question the results only lead to "How to access objects attributes" e.g this https://www.geeksforgeeks.org/accessing-attributes-methods-python/. And this one How to access attribute of object from another object's method, which is one of attributes in Python? uses inheritance.

2
  • Since this is Python there's probably a (somewhat convoluted) way to do it, but you should really rethink your code structure if your current structure presents this need. Commented Jul 30, 2020 at 19:32
  • I've been trying to restructure, but it ends up being, either fixed functions, classes that get ambiguously complex or a trade off where i use dictionary or list to store certain data. I don't feel comfortable with the using dictionary or list for certain data along with classes, is it OK if i do that? I don't know if its a bad practice. Commented Jul 30, 2020 at 20:04

2 Answers 2

1

Yes, you can pass the human into fire when it is created since they seem to be linked to one another:

class Human:
    def __init__(self):
        self.health = 100

class Fire:
    def __init__(self, human):
        self.fire = 10
        self.human = human
    def damage(self):
        self.human.health -= self.fire

bill = Human()
bill.fired = Fire(bill)
bill.fired.damage()   #the fired object damages bill object's health
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what's your goal, but as I mentioned, your issue looks like a code smell to me (an indication that something's not right).

Assuming you want the human instances to catch fire (i.e. create a fire instance) and then deduce the fire damage their health, consider the refactoring below:

class human:
    def __init__(self):
        self.health = 100
        self.fire = None

    def set_on_fire(self):
        self.fire = fire()

    def suffer_burn_damage(self):
        if self.fire is not None:
            self.health -= self.fire.damage

class fire:
    def __init__(self):
        self.damage = 10

bill = human()
print(bill.health)  # output: 100
bill.set_on_fire()
bill.suffer_burn_damage()
print(bill.health)  # output: 90

This way, you do not need the fire instances to know about the human's health in the first place. It's the human's "job" to keep track of whether it is burned or not, and when to deduce its own damage.

This makes sense in a more abstract meaning, too - which is one of the points of using OOP. A fire in real life has a certain energy. A human that catches fire will have its "health" deduced from whatever amount of energy that fire has. The fire itself has no business knowing about the human's health, or anything else for that matter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.