试图理解Python对象 [英] Trying to understand Python objects

查看:96
本文介绍了试图理解Python对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读像计算机科学家一样思考我不确定我是否理解

描述对象使用Python的方式。


1)属性可以添加到任何地方吗?我创建了一个名为

point的对象,然后我可以随时添加属性,并且在

程序中的任何地方?


2)课程通常是这样创建的:


class点数:

通过


然后添加属性在其他时间?


3)__underscores__是什么?


4)参数是否传递给类定义?


class Whatever(params):

pass


我理解对象使用PHP的方式。使用PHP,

类在一个地方定义 - 有点像函数。对我来说,

更有意义。

Reading "Think Like a Computer Scientist" I am not sure I understand
the way it describes the way objects work with Python.

1) Can attributes can added just anywhere? I create an object called
point, then I can add attributes any time, and at any place in the
program?

2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

3) What is with the __underscores__ ??

4) Are parameters passed to an class definition?

class Whatever(params):
pass

I sort-of understand the way objects work with PHP. With PHP, the
classes are defined in one place - sort of like a function. To me, that
makes more sense.

推荐答案

类不是函数。它们是一个东西。


一个简单的例子:


class pet(object):


def __init __(自我):

self.myLegs = self._SetNumberOfLegs()


def _SetNumberOfLegs(个体经营):

返回0


def HowManyLegsDoYouHav(个体经营):

打印self.myLegs


class cat(宠物):


def _SetNumberOfLegs(个体经营):

返回4


类兔子(宠物):


def _SetNumberOfLegs(个体经营):

返回2


类鱼(宠物):

通过


myPets = [cat(),fish(),rabbit()]


for myPets中的宠物:

pet.HowManyLegsDoYouHave()

在上面的例子中,猫和兔子都有腿和鱼?通常不是

。 :=)


walterbyrd写道:
classes are not functions. They are a "thing".

A simple example:

class pet(object):

def __init__(self):
self.myLegs=self._SetNumberOfLegs()

def _SetNumberOfLegs(self):
return 0

def HowManyLegsDoYouHave(self):
print self.myLegs

class cat(pet):

def _SetNumberOfLegs(self):
return 4

class rabbit(pet):

def _SetNumberOfLegs(self):
return 2

class fish(pet):
pass

myPets=[cat(), fish(), rabbit()]

for pet in myPets:
pet.HowManyLegsDoYouHave()
In the above example, both cat and rabbit has legs and fish? Not
normally. :=)

walterbyrd wrote:

阅读像计算机科学家一样思考我不确定我是否理解

描述对象使用Python的方式。


1)属性可以添加到任何地方吗?我创建了一个名为

point的对象,然后我可以随时添加属性,并且在

程序中的任何地方?


2)课程通常是这样创建的:


class点数:

通过


然后添加属性在其他时间?


3)__underscores__是什么?


4)参数是否传递给类定义?


class Whatever(params):

pass


我理解对象使用PHP的方式。使用PHP,

类在一个地方定义 - 有点像函数。对我来说,

更有意义。
Reading "Think Like a Computer Scientist" I am not sure I understand
the way it describes the way objects work with Python.

1) Can attributes can added just anywhere? I create an object called
point, then I can add attributes any time, and at any place in the
program?

2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

3) What is with the __underscores__ ??

4) Are parameters passed to an class definition?

class Whatever(params):
pass

I sort-of understand the way objects work with PHP. With PHP, the
classes are defined in one place - sort of like a function. To me, that
makes more sense.


在星期二21/11/2006 20:03,walterbyrd写道:
At Tuesday 21/11/2006 20:03, walterbyrd wrote:

> ; 1)属性可以添加到任何地方吗?我创建了一个名为
point的对象,然后我可以随时添加属性,并且在
程序的任何地方?
>1) Can attributes can added just anywhere? I create an object called
point, then I can add attributes any time, and at any place in the
program?



对于正常 python对象,是的,你可以随时随地添加/删除任何属性,

。有钩子,你可以主动拒绝

属性创建,或冻结属性列表,但这些是

被视为高级用法。

For "normal" python objects, yes, you can add/delete any attribute,
anytime, anywhere. There are hooks where you can actively deny
attribute creation, or "freeze" the attribute list, but these are
considered advanced usage.


> 2)类通常是这样创建的:

课程点数:

通过

然后在其他时间添加属性?
>2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?



不是通常!虽然你*可以*那样做,但并不代表你

*必须*这样做。

2D点可能会说:


类Point2D(对象):

def __init __(self,x,y):

self.x = x

self.y = y


def distance(self,other):

返回math.hypot(other.x-self.x,其他。 y-self.y)


p0 = Point2D(3,2)

p1 = Point2D(0,6)

p0.distance(p1)#给出5

Not "typically"! Although you *can* do that, doesn''t mean that you
*must* do things that way.
A 2D point might say:

class Point2D(object):
def __init__(self, x, y):
self.x = x
self.y = y

def distance(self, other):
return math.hypot(other.x-self.x, other.y-self.y)

p0 = Point2D(3, 2)
p1 = Point2D(0, 6)
p0.distance(p1) # gives 5


> 3)__underscores__是什么?
>3) What is with the __underscores__ ??



以__开头和结尾的名字由Python本身使用,并且

某些魔法有时附着。最常见的是__init__(用于

初始化一个新实例)。 __str__例如,用于获取适合打印的

对象的文本表示。如果执行

Names that begin and end in __ are used by Python itself and have
some "magic" attached sometimes. The most common is __init__ (used to
initialize a new instance). __str__ by example, is used to get an
object''s textual representation suitable for printing. If you execute


>> print p0
>>print p0



使用上面的例子,你会得到类似于< Point2D实例

在0x12345678>。将以下内容添加到Point2D类中,你会得到一个很好的一对:


def __str __(self):

return''(%f,%f) ''%(self.x,self.y)

using the example above, you''ll get something like <Point2D instance
at 0x12345678>. Add the following to the Point2D class and you get a nice pair:

def __str__(self):
return ''(%f, %f)'' % (self.x, self.y)


> 4)参数是否传递给类定义?

类随便(参数):

通过
>4) Are parameters passed to an class definition?

class Whatever(params):
pass



如果你看到类似的东西,params不是参数,而是基础

类。 Whatever类继承了所有已知的属性和方法

到它的基础,并且可能提供它自己的。

If you see something like that, "params" are not parameters, but base
classes. The Whatever class inherits all attributes and methods known
to its bases, and may provide its own.


>我有点理解对象使用PHP的方式。使用PHP,
类在一个地方定义 - 有点像函数。对我来说,
更有意义。
>I sort-of understand the way objects work with PHP. With PHP, the
classes are defined in one place - sort of like a function. To me, that
makes more sense.



通常在Python中你也只在一个地方定义一个类 - 你

*可以*稍后修改它,那就是'强大的功能,但肯定

你不需要!

-

Gabriel Genellina

Softlab SRL


__________________________________________________

Correo Yahoo!

Espacio para todos tus mensajes,antivirus y antispam?gratis!

?Abrítucuenta ya! - http://correo.yahoo.com.ar


walterbyrd写道:
walterbyrd wrote:

阅读像计算机科学家一样思考我不确定我是否理解

描述对象使用Python的方式。


1)属性可以添加到任何地方吗?我创建了一个名为

point的对象,然后我可以随时添加属性,并且在

程序中的任何地方?
Reading "Think Like a Computer Scientist" I am not sure I understand
the way it describes the way objects work with Python.

1) Can attributes can added just anywhere? I create an object called
point, then I can add attributes any time, and at any place in the
program?



并非所有对象都以这种方式运行。例如,对象的发生率

类:


pyj = object()

pyj.att = 2
--------------------------------------------- ---------------

Traceback(最近一次调用最后一次):

文件"< ipython console>",第1行,在< module>

< type''exceptions.AttributeError''>:''object''对象没有属性''att''

奇怪的是,确切的规则很难从行为中推断出来。

的cPython:


pydef doit():

....传递

....

pydoit.att = 2

pydoit.att

2

pytype(j)

< type''object''>

pytype(doit)

< type''function''>


您自己的班级实例以及您自己定义的班级表现如您所描述的那样
,虽然:


pyclass C:通过

....

py C.classatt = 2

pyC.classatt

2

pyc = C()

pyc.instat = 5

pyc.instat

5

Not all objects behave this way. For example, incidences of the object
class:

pyj = object()
pyj.att = 2
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type ''exceptions.AttributeError''>: ''object'' object has no attribute ''att''

Strangely enough, the exact rule is difficult to infer from the behavior
of cPython:

pydef doit():
.... pass
....
pydoit.att = 2
pydoit.att
2
pytype(j)
<type ''object''>
pytype(doit)
<type ''function''>

Instances of your own classes and the classes you define yourself behave
as you describe, though:

pyclass C: pass
....
pyC.classatt = 2
pyC.classatt
2
pyc = C()
pyc.instat = 5
pyc.instat
5


2)类通常是这样创建的:


class点数:

传递
2) Are classes typically created like this:

class Point:
pass



它更适合类从对象下降,实际上:


pyclass点(对象):传递

....

pytype(点)

< type''type''>

pytype(C)

< type''classobj''>


''type''类型类使用某些语言功能正常运行,

如属性。最后我查了''classobj''课程没有。所以

新风格(''type''类型)类是首选。

Its better for classes to descend from object, actually:

pyclass Point(object): pass
....
pytype(Point)
<type ''type''>
pytype(C)
<type ''classobj''>

''type'' type classes behave correctly with certain language features,
such as properties. Last I checked ''classobj'' classes do not. So
new-style (''type'' type) classes are preferred.


然后在其他时间添加属性?
Then attributes are added at some other time?



是的。这适用于旧(clasobj)和新风格类。

Yes. This goes for old (clasobj) and new style classes.


3)__underscores__是什么?
3) What is with the __underscores__ ??



它们很难看,但它们意味着表示实现

对象的特定属性,而不是特定于接口的属性。 />
但是,在我看来,很多python代码,特别是更高级别的代码。代码

模糊了区别,所以它变得有点下划线重。

They are hard to look at, but they are meant to signify implementation
specific attributes of objects and not interface specific attributes.
But, in my opinion, a lot of python code, especially "higher-level" code
blurs the distinction, so it becomes a little underscore heavy.


4)参数是否传递给类定义?


class Whatever(params):

通过
4) Are parameters passed to an class definition?

class Whatever(params):
pass



No.


这是在名为__ init __的实现特定方法下完成的。


pyclass D(对象):

.... def __init__ (self,param1,param2):

.... self.a = param1

.... self.b = param2

....

pyd = D(2,5)

pyd.a

2

pyd。 b

5

No.

This is done under the implementation specific method called "__init__".

pyclass D(object):
.... def __init__(self, param1, param2):
.... self.a = param1
.... self.b = param2
....
pyd = D(2,5)
pyd.a
2
pyd.b
5


我理解对象使用PHP的方式。使用PHP,

类在一个地方定义 - 有点像函数。对我来说,

更有意义。
I sort-of understand the way objects work with PHP. With PHP, the
classes are defined in one place - sort of like a function. To me, that
makes more sense.



差异(我不知道PHP)是你创建的许多对象(类和实例)类)是可变的。

意味着,当然你可以改变它们。我发现良好的编程实践通常需要较少的这种可变性(即

即时添加属性),但是如果你需要的话它就在那里。我认为

这种灵活性是蟒蛇哲学的一部分,但我不是蟒蛇

哲学家(也不是我的意思)。


James

-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

Box 951570

洛杉矶,CA 90095

http ://www.jamesstroud.com/


这篇关于试图理解Python对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆