python中的属性和方法_Python中类属性、实例属性和实例方法之间的区别

我知道以前也有人问过类似的问题,但每个网站对这些问题的定义似乎都不一样,我正试图遵循官方文档:就我目前所知:类属性是class Classname:代码块中列出的所有内容,包括__init__函数上面的变量声明、静态方法、类方法和包含self实例的方法更多信息链接:https://docs.python.org/2/tutorial/classes.html#class对象实例属性都是:1.)每个函数都列在class Classname:代码块中,所以除了__init__函数和amp;数据属性,当我们使用instance.attr并将其设置为某个值时更多信息链接:https://docs.python.org/2/tutorial/classes.html#instance-objects

假设上面所写的都是正确的,那么实例方法就是class Classname:代码块中的任何函数,因为我们可以将每个方法应用到实例对象中,或者它可以只是包含self的函数

我不确定我理解错了什么,但是python文档的术语与我所见过的其他站点完全不同。任何帮助都将非常感谢。谢谢!

类的所有实例的类属性都是相同的,而每个实例的实例属性都是特定的。实例属性是针对每个实例的特定数据的,类的所有实例都应该使用该类的类属性。

"实例方法"是一个特定的类属性,它接受类的实例作为第一个属性,并假定对该实例进行操作。

"类方法"是类中定义的一个方法,它接受类作为第一个属性而不是实例(这就是为什么are类方法)。

你可以通过访问a.b. dict__很容易地看到类属性:

1

2

3

4

5

6

7class A:

class_attribute = 10

def class_method(self):

self.instance_attribute = 'I am instance attribute'

print A.__dict__

#{'__module__': '__main__', 'class_method': , 'class_attribute': 10, '__doc__': None}

实例属性为A().__dict__:

1

2

3

4a = A()

a.class_method()

print a.__dict__

# {'instance_attribute': 'I am instance attribute'}

一些来自官方python文档的有用链接,我希望这些链接不会让您更加困惑,并给出更清晰的理解……Python类进入python, 5.8。引入类属性python getattr和setattr的定义Python中的类方法是用来做什么的?类和实例方法之间的区别

完美的答案。谢谢!

要回答这个问题,首先需要考虑python如何查找属性。我假定您知道实例是什么。作为复习…

1

2

3

4

5

6

7

8

9

10

11class SomeClass(object):

class_attribute = 'This -- Defined at the class level.'

def __init__(self):

self.instance_attribute = 'This -- Defined on an instance.'

def method(self):

pass

instance = SomeClass()

类属性是在类级别定义的。实例属性在实例级定义(通常通过self.xyz = ...)。实例属性也可以被monkey应用到实例上:

1instance.another_instance_attribute = '...'

注意,如果实例属性没有"影子"它,则可以在实例上查找类属性。

1print(instance.class_attribute)

现在的方法。方法完全是另一种动物。

1SomeClass.method

绝对是类的属性。

1instance.method

分类有点棘手。实际上,当python看到这个语句时,它实际上执行以下操作:

1SomeClass.method.__get__(instance, SomeClass)

奇怪。这里发生的是函数是描述符。因为它们有一个__get__方法,所以会调用它,并且返回值(在本例中)是一个知道self是什么的新函数。通常的术语是"实例方法"和"绑定方法"。我想有些人可能认为它是一个实例属性,但我不确定这是否完全正确(即使您通过实例访问它)

你似乎对这些概念掌握得很好。很难找到以通用方式解释的信息,特别是像python这样使用广泛的语言。我主要是附和您的正确假设,并在此过程中进行一些小的调整。

类属性是所有将从类创建的对象共享的特征。例如,如果我们定义一个clock类,我们将定义一个Class属性为时钟中可能的总小时数。

1

2class Clock:

class_hours = 12

它由类的所有实例共享,任何实例都可以访问它以供参考。

类实例是我们用来定义从类创建的对象的个体特征的实际数据。再次使用clock类的例子,这将是我们在一行代码中创建时钟时设置时钟的实际小时(我们还可以引用我们的类属性,以确保它在可接受的参数内):

1

2

3

4

5

6class Clock:

class_hours = 12

def __init__(self, hours):

if (hours <= class_hours):

self.hours = hours

请记住,在构造函数中使用if语句不是很好的实践,但这只是为了举例说明。如果您真的想实现这个示例,您将使用一个实例方法来确保时间在定义的范围内。

实例方法是我们在类中定义的方法,它可以操作我们在构造函数中定义的类实例数据。再次回到clock的例子,我们可以定义一个方法来改变时钟的小时、分钟或秒:

1

2

3

4

5

6

7

8

9

10

11

12

13

14class Clock:

class_hours = 12

class_minutes = 60

class_seconds = 60

def __init__(self, hours, minutes, seconds):

self.hours = hours

self.minutes = minutes

self.seconds = seconds

def setTime(new_hour, new_minute, new_second):

self.hours = new_hour

self.minutes = new_minute

self.seconds = new_second

这允许我们更改代码中已经创建的clock的实例。例如,调用clock.setTime(11,49,36)将把时钟的特定实例设置为11:49:36。

我希望这对您有帮助,并且尽可能清晰。如果我含糊不清或忽略了您不理解的任何术语,请让我知道,以便我可以更新我的答案,使其尽可能清晰。

谢谢你的回复!我的主要问题是文档中有一行字:"根据定义,作为函数对象的类的所有属性都定义了其实例的相应方法。另一个关于类属性的部分说类属性是类下面名称空间中的任何东西,包括涉及self的函数。那么,类属性不是类代码块中的所有内容,实例方法不是类代码块中的所有函数吗?我理解你上面所写的,但这似乎与网站上所写的大相径庭。

Class attributes-

are everything listed in the class Classname: code block, It can be above or below of any method.

Instance attribute - which is created by using instance.attr or self.attr(here self is instance object only)

1

2

3

4

5

6

7class Base:

cls_var ="class attribute"

def __init__(self):

self.var ="instance attribute"

def method(self):

self.var1 ="another instance attribute"

cls_var1 ="another class attribute"

Class attributes and instance objects - You can consider class object as a parent of all of its instance objects. So instance object can access attribute of class object.However any thing defined under class body is class attribute only.

1

2

3

4

5

6

7

8

9

10

11class Base:

cls_var ="class attribute"

def __init__(self):

self.var ="instance attribute"

b = Base()

print b.var                                     # it should print"instance attribute"

print b.cls_var                                 # it should print"class attribute"

print Base.cls_var                              # it should print"class attribute"

b.cls_var ="instance attribute"

print Base.cls_var # it should print"class attribute"

print b.cls_var # it should print"instance attribute"

Instance Method - Method which is created by def statement inside class body and instance method is a Method which first argument will be instance object(self).

But all instance methods are class attributes, You can access it via instance object because instances are child of class object.

1

2

3

4

5

6

7

8

9

10>>> class Base:

def ins_method(self):

pass

>>> b = Base()

>>> Base.__dict__

{'__module__': '__main__', '__doc__': None, 'ins_method': }

>>> b.__dict__

{}

您可以通过使用类对象来访问实例方法,方法是将实例作为它的第一个参数传递

1Base.ins_method(Base())

你可以参考我写的这两篇关于同一主题的文章,其中有图表说明,并把你的疑问-http://www.pythonabc.com/python-class-easy-way-understand-python-class/http://www.pythonabc.com/instance-method-static-method-and-class-method/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值