tutizeri
tutizeri

Reputation: 609

Why this function doesn't return the variable

This class has a function name, which should return the dog's name ("Boby" in following the example).

class Dog(object):
    def __init__(EstePerro, Nombre=None, Peso=None):
        EstePerro._Name = Nombre 
        EstePerro._Weight = Peso
    def name(EstePerro):
        "Returns dog name"
        return EstePerro._Name

pet = Dog("Boby",45)
print(pet.name)

But instead it returns this:

<bound method Dog.name of <__main__.Dog object at 0x0000000002DD4748>>

What am I doing wrong?

Upvotes: 0

Views: 47

Answers (1)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

  • pet.name is your method
  • pet.name() is a call to your method

So here, you need to add parenthesis to call your name method:

>>> pet = Dog("Boby", 45)
>>> print(pet.name()) # Parentheses after pet.name
Boby

Upvotes: 2

Related Questions