Ex40 and ex42 - How to call definitions from classes?

I am working on ex42, and do not understand how calling a def from a class works.

Trying to simplify ex40, if I call a def this way:

class foo(object):
    def __init__(self, bar):
        self.bar = bar

    def print_bar(self):
        print(bar)

stuff = (foo(bar("test")))

stuff.print_bar()

The result is NameError: name 'bar' is not defined

In ex42, if I change the Animal class to

class Animal(object):
    def __init__(self, animal):
        self.animal = animal

And add to the Person class

def pet_name(self):
        print(name, "has a pet", animal, "named", pet)

to the Person class, and call it with mary.pet_name() I get the same error of NameError: name 'pet_name' is not defined

What part of the logic am I not understanding?

Take a look at stuff.
What do you want stuff to be?
You want to create an instance of the class foo? And “test” would be your bar?
If this is what you want, you do:
stuff = foo("test")
In your print_bar() function you need to do:
print(self.bar), because you have no variable called bar.
P.S. show me the whole code for Person and how you instantiate it ( the line where mary appears for the first time)

Thank you for your answer! Here is my code modified from ex42:

class Animal(object):
    def __init__(self, animal):
        self.animal = animal

class Person(object):

    def __init__(self, name):
        # self has-a name as name
        self.name = name

        # Person has-a pet of some kind
        self.pet = None

    def pet_name(self, name):
        print("has a pet", animal, "named", pet)

mary = Person("Mary")

mary.pet = satan

this is then called with:

mary.pet_name(satan)

Resulting in the error:

NameError: name 'animal' is not defined

You didn’t instantiate class Animal.
You need to do:

mary = Person("Mary")  
animal = Animal("cat")  
mary.pet = "satan"`

Now you can call mary.pet_name(mary.pet)
P.S.
are you sure you don’t have a class Cat(Animal) in ex42?

Exercise 42 doesn’t go in much details about building functions inside of classes. I think Zed mainly wants you to distinguish between a class and an object.

Later on, in exercise 44d and 44e you will start seeing on how to use functions within classes. So I wouldn’t treat myself too hard on learning the relationship between classes and functions just yet.

However, if you want to get your code working, you could use this next code as a reference:

In regards of ex40.py

class foo(object):

    # 1.foo has-a attribute named bar.
    def __init__(self, bar):
        self.bar = bar

    # function prints the attribute inside the print stateme
    def print_bar(self):
        print(self.bar)

You create a variable named ‘stuff’ which is an instance of the ‘foo’ class and “test” is given as argument in reference to ‘bar’:

stuff = foo("test")

You can now use the functions from the ‘foo’ class within your variable stuff:

stuff.print_bar()

Now the previous will print:

test

because that is what is referenced to the
attribute ‘bar’ which is inside the

.print_bar()

function.

In regards of ex42.py

Parent class:

class Animal(object):
    def __init__(self, animal):
        self.animal = animal

Cat class now inherited functionalities from the Animal class:

class Cat(Animal):
    def __init__(self, name):
        # Cat has-a attribute named name
        self.name = name

satan = Cat("Satan")

Person class now inherited functionalities from the Animal class

class Person(Animal):

    # Person has-a name and a pet attribute
    def __init__(self, name):

        # self has-a name as name
        self.name = name

        # Person has-a pet of some kind
        self.pet = None

    def pet_name(self, name):
        print(self.name, "has a pet named", name)

mary = Person("Mary")

mary.pet = satan.name

mary.pet_name(mary.pet)

That should print:

Mary has a pet named Satan

2 Likes

Hey @heavywind, the answers here are good, and @Alfredo13 is correct that I don’t quite cover that in this exercise and you should go to the next one. However, to answer your question simply:

  1. You need to use the class to create an object. The class is kind of like the blueprints for how objects are made. You then create an object based on that blueprint with: mary = Animal()
  2. Once you have an object, you just use the . to call functions you added in the class with def. Read that sentence 10 times and notice where I used class and object. So in the above I’d do mary.pet_name().
  3. In your functions, you need to access the current object. Remember that a class is just the blueprint for the objects. The function has no idea what object it needs to work on, so that’s why self is added to every call to make it so your function knows what object to modify. In your pet_name you just need self.name and self.pet to make it work.

Try that, then continue on so you get more knowledge, and then come back with more questions. These are good questions.

2 Likes

Thanks Zed, and @Alfredo13 and @io_io . I have some more questions, but will move on to the other exercises first to see if I can understand them from there :thinking:

1 Like