Let's say I want to create two classes Owner and Pet

class Owner(object):
    def __init__(first_name, last_name, address, phone):
        self.first_name = first_name
        self.last_name = last_name
        self.address = address
        self.phone = phone

class Pet(object):
    def __init__(name, owner)
        self.name = name
        self.owner = Owner.__init__(first_name, last_name, address, phone)

Did I pass the class Owner as a parameter to the class Pet correctly or not? If not, then how is it done properly? Thanks.

6 Years
Discussion Span
Last Post by pyprog

You didn't do it properly the way you set it up. You can do one of a few ways. I will show you the two I use

1) change line 11 to

self.owner = owner

Then when you are running it, create an Owner first, then pass that into the Pet.

x = Owner("Bob", "Marley", "Address", "111-111-1111"
y = Pet("Spot", x)

2) Change the Pet class to this

class Pet(object):
    def __init__(self, name, first_name, last_name, address, phone):
        self.name = name
        self.owner = Owner(first_name, last_name, address, phone)

Also, your "__init__" needs to have "self" as the 1st parameter.
like this.

def __init__(self, other_parameters):

self name for the first parameter is just convention, but mostly it is sensible to use that name for that for clarity.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.