Yusuf Ning
Yusuf Ning

Reputation: 65

class and methods in python. introduction in oop

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(myname):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(myname)

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())

its suppose to produce "Hi, my name is Mark." or "Hi, my name is Steve."

but instead it produces "Hi, my name is undefined."

Upvotes: 0

Views: 109

Answers (3)

DeepSpace
DeepSpace

Reputation: 81594

It should be printing the object's representation in memory (something along the lines of Hi, my name is <__main__.Person object at 0x005CEA10>).

The reason is that the first argument of a method is expected to be the object that the method is called upon.

Just like you have def __init__(self, name): you should have def introduction(self, myname):.

Then you will encounter another problem, as introduction now expects an argument myname which you don't provide. However, it is not needed now since you have access to self.myname.

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)


# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())

Will output

Hi, my name is Mark.
Hi, my name is Steve.

Upvotes: 3

ospahiu
ospahiu

Reputation: 3525

You need to declare introduction() -> introduction(self) as an instance method (by passing in self) to be able to access the instance variable self.myname.

class Person:

    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is {}.".format(self.myname)

Sample output:

# Use the class to introduce Mark and Steve
mark = Person("Mark")
steve = Person("Steve")

print(mark.introduction())
print(steve.introduction())
>>> Hi, my name is Mark.
>>> Hi, my name 

Please note however, that the first parameter in a function within a class is reserved for either a class, or object to pass itself to (unless a @staticmethod tag is applied to the method, then the first implicit parameter is not passed; which essentially behave as module methods).

Also keep in mind that self is not a reserved word, so you could name it anything (even though self is PEP convention). The below example executes the same output as the example above, and is semantically the same.

def introduction(myname):
    """Returns an introduction for this person."""
    return "Hi, my name is {}.".format(myname.myname)

9.3.5. Class and Instance Variables

Upvotes: 1

Chris
Chris

Reputation: 22953

Your problem is that your giving your introduction method the parameter myname, but never supplying it with a valid argument.You can simply do:

mark = Person(" Mark")
steve = Person(" Steve")

print(mark.introduction(mark.myname))
print(steve.introduction(steve.myname))

your giving the introduction method, the variable from your class myname.

But the above is not even necessary. Since your initializing your name variable in the __init__ method of your class, it is like a global variable. So you can simply say:

class Person:
    def __init__(self, name):
        """Make a new person with the given name."""
        self.myname = name

    def introduction(self):
        """Returns an introduction for this person."""
        return "Hi, my name is{}".format(self.myname)


# Use the class to introduce Mark and Steve
mark = Person(" Mark")
steve = Person(" Steve")

print(mark.introduction())
print(steve.introduction())

Upvotes: 0

Related Questions