-3

I've tried to make an OOP based program in python. I gave it an object to work with and tried to make it print the name, but its not working.

class human:
    def __init__(self, name):
        print("this is a human")
    def name(self, name):
        print("this is {}".format(bob.name))

bob = human("bob")

Anyone know what the problem could be?

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Heech.p
  • 51
  • 8
  • Does this answer your question? [Getting the class name of an instance?](https://stackoverflow.com/questions/510972/getting-the-class-name-of-an-instance) – ai.jennetta Jun 19 '20 at 12:00
  • 2
    @ai.jennetta It does not. – deceze Jun 19 '20 at 12:07
  • @ai.jennetta thanks for the recommendation, but it either doesn't help me at all, as I can't understand it, or it's in a format where it's difficult for me to understand – Heech.p Jun 19 '20 at 12:09
  • 1
    Welcome to SO! What is the desired behaviour? Perhaps you want one of the following: 1. Print `human` on initializing (`bob = human("bob")`). 2. Print `bob` on initializing. 3. Print `bob` on calling `name` method (`bob.name()`). – Sergey Shubin Jun 19 '20 at 12:25
  • I'm not sure why my question got downvoted, could someone explain why they downvoted so i could fix the error? – Heech.p Jun 20 '20 at 08:46

4 Answers4

3

Beyond the answers you already received (which solve your problem), I'd suggest not having a method that prints the name. Rather, you should have a __str___ dunder method that defines the object's behavior when an instance is printed.

class human:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return self.name

person = human("bob")

print(person)
'bob'

You can also define the object's behavior when the instance name is entered in the console, for instance just running the line

>>> person

You can do it with __repr__:

def __repr__(self):
    return f'when entering the instance name in the console: {self.name}'

This will print:

when entering the instance name in the console: bob

This appears more pythonic to me than having a method that simply prints the name.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
2
  1. You're never storing the name on the instance, where would it get the name from? Your __init__ needs to do something along the lines of self.name = name
  2. the name method and attribute are going to conflict, the latter will shadow (hide) the former, and it should look up whatever attribute its using on self
Masklinn
  • 34,759
  • 3
  • 38
  • 57
1

You never assigned the passed name to the object. Try:

class human:

    def __init__(self, name):
        print("this is a human")
        self.name = name
    def print_name(self):
        print("this is {}".format(self.name))
bob = human("bob")
bob.print_name()
emluk
  • 261
  • 1
  • 8
1

there are couple of things to update in the code:

  • bob is an instance which is not defined at human class
  • notice that init, name functions expect external param but you never use it in the function. (in self. = name) in order to use it: define a var in the class named 'name' and update you function to:
class human:
    _name = ""
    def __init__(self, name):
        print("this is a human")
        self._name = name
    def name(self):
        print("this is "+ self._name)

bob = human("bob")
bob.name()
  • bob = human("bob") only init function and you should call bob.name() in order to call the print-name function
minion
  • 1,889
  • 1
  • 8
  • 9