方法和成员变量之间的区别?

时间:2021-06-26 23:10:39
class Animal(object):
    """Makes cute animals."""
    is_alive = True
    health = 'good'
    def __init__(self, name, age):
        self.name = name
        self.age = age
    # Add your method here!
    def description(self):
        print self.name
        print self.age

hippo = Animal('Tom', '20')
sloth = Animal('Randy', '18')
ocelot = Animal('Birdman','57')
hippo.description()
print ocelot.health
print hippo.health
print sloth.health

The code above is from codecademy's python course. I am getting confused about some of the definitions surrounding OOP. If my understanding is correct, a function defined within a class is known as a method, which is why when it's called, for example like this: 'hippo.description()', the '()' are necessary because of the syntax involving functions.

上面的代码来自codecademy的python课程。我对围绕OOP的一些定义感到困惑。如果我的理解是正确的,那么在类中定义的函数被称为方法,这就是为什么在调用它时,例如像:hippo.description()','()'是必要的,因为语法涉及功能。

However, I start to get confused with 'self.name' and 'self.age'. Are these also methods? I was wondering if they were perhaps member variables, but then wouldn't they be defined in the same way the variable 'health' was above? And if they aren't member variables, how come they can be accessed using dot notation in the same way as the member variables?

但是,我开始对'self.name'和'self.age'感到困惑。这些也是方法吗?我想知道它们是否可能是成员变量,但是它们不会以变量“健康”在上面的方式定义吗?如果它们不是成员变量,为什么使用点符号以与成员变量相同的方式访问它们?

Cheers

2 个解决方案

#1


0  

I assume you're coming from a more traditional OOP programming language like C++ or Java.

我假设您来自更传统的OOP编程语言,如C ++或Java。

health in the Animal class is what you would refer to as a static member variable, but in Python this is called a class attribute because it is unique to the class.

Animal类中的health是你所谓的静态成员变量,但在Python中,这称为class属性,因为它对于类是唯一的。

name in the Animal class is what you would refer to as a member or instance variable, and in Python this is called an instance attribute because it is unique to each instance of a class.

Animal类中的名称是您将其称为成员或实例变量的名称,而在Python中,这称为实例属性,因为它对于每个类的实例都是唯一的。

You use self to refer to attributes within its own class.

您可以使用self来引用其自己的类中的属性。

#2


0  

First of all the difference between class and instance attributes are answered elsewhere.

首先,类和实例属性之间的区别在其他地方得到解答。

The difference between a method and member variables are that while they are both attributes, a method is a function while a member variable is not (or need not be). Also a method is normally a class attribute (at least if you use new style classes).

方法和成员变量之间的区别在于,虽然它们都是属性,但方法是函数,而成员变量不是(或不必是)。方法通常也是一个类属性(至少如果你使用新的样式类)。

However in python functions are first class objects so this may confuse a little more: it's perfectly valid to assign a member variable with a function (or vice versa), but then that will become somewhat different because normally a method is shared among all objects, but when assigned to an instance it becomes private to that instance.

但是在python函数中是第一类对象,所以这可能会混淆一点:用一个函数分配一个成员变量是完全有效的(反之亦然),但那会变得有些不同,因为通常一个方法在所有对象之间共享,但是当分配给实例时,它变为对该实例的私有。

self.foo may be used to access both instance attributes or class attributes (if instance attribute does not exist).

self.foo可用于访问实例属性或类属性(如果实例属性不存在)。

#1


0  

I assume you're coming from a more traditional OOP programming language like C++ or Java.

我假设您来自更传统的OOP编程语言,如C ++或Java。

health in the Animal class is what you would refer to as a static member variable, but in Python this is called a class attribute because it is unique to the class.

Animal类中的health是你所谓的静态成员变量,但在Python中,这称为class属性,因为它对于类是唯一的。

name in the Animal class is what you would refer to as a member or instance variable, and in Python this is called an instance attribute because it is unique to each instance of a class.

Animal类中的名称是您将其称为成员或实例变量的名称,而在Python中,这称为实例属性,因为它对于每个类的实例都是唯一的。

You use self to refer to attributes within its own class.

您可以使用self来引用其自己的类中的属性。

#2


0  

First of all the difference between class and instance attributes are answered elsewhere.

首先,类和实例属性之间的区别在其他地方得到解答。

The difference between a method and member variables are that while they are both attributes, a method is a function while a member variable is not (or need not be). Also a method is normally a class attribute (at least if you use new style classes).

方法和成员变量之间的区别在于,虽然它们都是属性,但方法是函数,而成员变量不是(或不必是)。方法通常也是一个类属性(至少如果你使用新的样式类)。

However in python functions are first class objects so this may confuse a little more: it's perfectly valid to assign a member variable with a function (or vice versa), but then that will become somewhat different because normally a method is shared among all objects, but when assigned to an instance it becomes private to that instance.

但是在python函数中是第一类对象,所以这可能会混淆一点:用一个函数分配一个成员变量是完全有效的(反之亦然),但那会变得有些不同,因为通常一个方法在所有对象之间共享,但是当分配给实例时,它变为对该实例的私有。

self.foo may be used to access both instance attributes or class attributes (if instance attribute does not exist).

self.foo可用于访问实例属性或类属性(如果实例属性不存在)。