类方法与函数 [英] class methods vs. functions

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

问题描述



定义类的方法与定义

的优缺点是什么?这个函数将类的实例作为参数?在下面的示例

中,是否能够更好地编写

z.modulus()或模数(z)?在这方面,这对于
的Python和C ++有区别吗?


来自数学导入sqrt

class xy:

def __init __(self,x,y):

self.x = x

self.y = y

def模数(个体积):

返回sqrt(self.x ** 2 + self.y ** 2)


def模数(z):

返回sqrt(zx ** 2 + zy ** 2)


z = xy(3,4)

print z.modulus ()

打印模数(z)


---- ==通过Newsfeed.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== - -
http://www.newsfeed.com #1新闻组服务在世界上! > 100,000新闻组

--- = 19东/西海岸专业服务器 - 通过加密的总隐私= ---

解决方案

2004年7月14日, be*******@aol.com 写道:

定义一个类的方法与定义一个以类的实例作为参数的函数有什么优缺点?在下面的例子中,是否能够更好地编写z.modulus()或模数(z)?在这方面,Python和C ++之间有区别吗?


对于计算机科学中的大多数问题,这个问题的答案是它取决于...... b $ b取决于。 :)答案(部分是我的意见)与

接口有关,以及你想要完成的事情:


- 案例1 -


所有对象都符合''xy''界面。此接口

重新查询对象具有.x和.y成员变量。然后你可以定义一个''模数''函数,它被定义为对任何对象进行操作

实现''xy''界面:

def模数(z):
返回sqrt(zx ** 2 + zy ** 2)


上行:


无论物体是什么,或者它来自何处,只要它实现''xy''界面,你现在就可以得到它的模数。


下行:


你不能采用任何不实现xy的模数。


- 案例2 -


你的一些对象实现了''xy''界面,其他人没有(例如他们

don用它来说是有道理的。无论哪种方式,您希望能够找到他们的

模数。您可以通过使所有对象符合

''模数''界面来实现此目的。这个界面要求一个对象有一个

..模数()成员函数:

来自数学导入sqrt
类xy:
def __init __( self,x,y):
self.x = x
self.y = y
def模数(self):
返回sqrt(self.x ** 2 + self .y ** 2)




上行:


您可以获取您创建的任何对象的模数,无论是否

它可以实现''xy''界面。


下行:


如果你不是对象的设计者,即使它确实实现了''xy''界面,你也不能把它的模数设为



所以,选择取决于你。但是你可以随时提供两种:


def mymodulus(z):

试试:

返回z.modulus()

除了AttributeError:

返回模数(z)


如果你不能告诉我,我是在你这个问题上犹豫不决;)


2004年7月14日星期三03:38:40 -0400,Christopher T King写道:

2004年7月14日, be*******@aol.com 写道:

定义类的方法与定义
将类的实例作为参数的函数有什么优缺点?


从技术角度来看,克里斯托弗的解释似乎没问题,



但我对面向对象编程的有限研究告诉我

来自在对象外部,你不应该直接使用它的数据属性。

相反,你应该发送对象消息ges,即使用这个对象的方法。

所以z.modules()似乎是更好的主意,而模块(z)则更糟糕。


我想这意味着你应该将

数据属性的每个名称foo视为__foo。

egbert

-

Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991

=================== =============================== =================== ===


" be ******* @ aol.com" <是******* @ 127.0.0.1:7501>写道:

定义一个类的方法与定义一个以类的实例作为参数的函数有什么优缺点?在下面的例子中,是否能够更好地编写z.modulus()或模数(z)?




你'基本上将消息传递范例与

泛型函数范例进行比较。


消息传递的一个优点是它将消息分开/>
进入名称空间,所以你不必小心选择你的

消息名称。


消息的一个缺点-passing,是它将消息

分隔成名称空间,所以你不太注意选择你的消息

名字,并最终让你的客户感到困惑。 :-)


通用功能的一个优点是多次发送更加自然(我敢说,更容易吗?)。


通用功能的一个缺点是,如果你不小心,

你的代码最终分布在整个商店。



What are the pros and cons of defining a method of a class versus defining
a function that takes an instance of the class as an argument? In the example
below, is it better to be able to write
z.modulus() or modulus(z)? Is there a difference between Python and C++ in
this regard?

from math import sqrt
class xy:
def __init__(self,x,y):
self.x = x
self.y = y
def modulus(self):
return sqrt(self.x**2 + self.y**2)

def modulus(z):
return sqrt(z.x**2 + z.y**2)

z = xy(3,4)
print z.modulus()
print modulus(z)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

解决方案

On 14 Jul 2004, be*******@aol.com wrote:

What are the pros and cons of defining a method of a class versus defining
a function that takes an instance of the class as an argument? In the example
below, is it better to be able to write
z.modulus() or modulus(z)? Is there a difference between Python and C++ in
this regard?
The answer to this, as for most questions in computer science, is "it
depends". :) The answer (which is partially my opinion) has to do with
interfaces, and what you want to accomplish:

- Case 1 -

All of your objects conform to the ''xy'' interface. This interface
requeries that an object has both .x and .y member variables. You can then
define a ''modulus'' function that is defined to operate on any object
implementing the ''xy'' interface:
def modulus(z):
return sqrt(z.x**2 + z.y**2)
Upside:

No matter what the object is, or from where it comes, so long as it
implements the ''xy'' interface, you can now take the modulus of it.

Downside:

You can''t take the modulus of anything that doesn''t implement ''xy''.

- Case 2 -

Some of your objects implement the ''xy'' interface, others don''t (e.g. they
don''t make sense with it). Either way, you want to be able to find their
modulus. You can do this by making all your objects conform to the
''modulus'' interface. This interface requires that an object have a
..modulus() member function:
from math import sqrt
class xy:
def __init__(self,x,y):
self.x = x
self.y = y
def modulus(self):
return sqrt(self.x**2 + self.y**2)



Upside:

You can take the modulus of any object you create, regardless of whether
it can implement the ''xy'' interface or not.

Downside:

If you aren''t the designer of the object, you can''t take the modulus of
it, even if it does implement the ''xy'' interface.

So, the choice is up to you. You can always provide both, though:

def mymodulus(z):
try:
return z.modulus()
except AttributeError:
return modulus(z)

In case you can''t tell, I''m as undecided on the issue as you ;)


On Wed, Jul 14, 2004 at 03:38:40PM -0400, Christopher T King wrote:

On 14 Jul 2004, be*******@aol.com wrote:

What are the pros and cons of defining a method of a class versus defining
a function that takes an instance of the class as an argument?

From a technical point of view Christopher''s explanation seems to be OK,


but my limited studies of object oriented programming taught me that
from outside an object you should not use its data-attributes directly.
Rather you should send the object messages, ie use methods of this object.
So z.modules() seems to be the better idea, and modules(z) the worse one.

I suppose that this means that you should treat each name foo of a
data-attribute as if it was called __foo.
egbert
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================


"be*******@aol.com" <be*******@127.0.0.1:7501> writes:

What are the pros and cons of defining a method of a class versus defining
a function that takes an instance of the class as an argument? In the example
below, is it better to be able to write
z.modulus() or modulus(z)?



You''re essentially comparing the message-passing paradigm to the
generic function paradigm.

One advantage of message-passing, is that it separates the messages
into namespaces, so you don''t have to be as careful about chosing your
message names.

One disadvantage of message-passing, is that it separates the messages
into namespaces, so you are less careful about chosing your message
names, and end up confusing your clients. :-)

One advantage of generic functions is that multiple dispatch is much
more natural (dare I say, easier?).

One disadvantage of generic functions is that, if you are not careful,
your code ends up distributed all over the shop.


这篇关于类方法与函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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