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.

Recommended Answers

All 3 Replies

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)

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

OK. Thanks a lot, you guys. Appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.