2

I have two classes. When I create an instance in the first one would like to work with its attributes in my second class.

class Dog(object):
    def __init__(self, number):
        self.number_of_tricks = number

class Statistics(object):
    def get_number_of_tricks(self):
        return Dog(Max.number_of_tricks)

Now I create a instance >>> Max = Dog(15) and want the class "Statistics" be able to gain its value "number_of_tricks". For example something like >>> stat = Statistics(), >>>stat.get_number_of_tricks()

2
  • 1
    Why did you change self to snelf in get_number_of_tricks? Commented Jul 9, 2016 at 16:43
  • a typing error, thanks for the notification Commented Jul 9, 2016 at 17:02

1 Answer 1

5

Initialize your Statistics with that instance of Dog, then the attributes of that Dog will be accessible from Statistics:

class Dog(object):
    def __init__(self, number):
        self.number_of_tricks = number

class Statistics(object):
    def __init__(self, dog_instance):
        self.dog = dog_instance

    def get_number_of_tricks(self):
        return self.dog.number_of_tricks

And you can do:

>>> from my_module import Dog, Statistics
>>> Max = Dog(15)
>>> stat = Statistics(Max)
>>> stat.get_number_of_tricks()
15
Sign up to request clarification or add additional context in comments.

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.