简体   繁体   English

python中的类和方法。 oop介绍

[英]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." 而是显示“嗨,我的名字未定义”。

It should be printing the object's representation in memory (something along the lines of Hi, my name is <__main__.Person object at 0x005CEA10> ). 它应该在内存中打印对象的表示形式(类似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): . 就像您拥有def __init__(self, name):您应该拥有def introduction(self, myname):

Then you will encounter another problem, as introduction now expects an argument myname which you don't provide it. 然后,您将遇到另一个问题,因为现在introduction期望您不提供参数myname You don't actually need it since you now have access to self.myname . 实际上,您实际上不需要它,因为您现在可以访问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.

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 . 您需要将Introduction introduction() -> self.myname introduction(self)为实例方法( 通过传入 self ),以便能够访问实例变量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 ). 但是请注意, 类中函数中的第一个参数是为类或对象保留的,以将其自身传递给( 除非将 @staticmethod 标记应用于该方法,否则不会传递第一个隐式参数;这实质上是表现为模块方法 )。

Also keep in mind that self is not a reserved word, so you could name it anything ( even though self is PEP convention ). 还要记住, self不是保留字,因此您可以给它起任何名字( 即使 self 是PEP约定 )。 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. 9.3.5。 Class and Instance Variables 类和实例变量

Your problem is that your giving your introduction method the parameter myname , but never supplying it with a valid argument.You can simply do: 您的问题是您为介绍方法指定了参数myname ,但从未为其提供有效的参数。您可以执行以下操作:

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 . 您提供的介绍方法是类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. 由于您是在类的__init__方法中初始化名称变量的,因此它就像一个全局变量。 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM