OO惯例 [英] OO conventions

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

问题描述

我对面向对象编程比较新,所以偶尔会混淆

关于它的用法。假设有一个类Image,

有多种方法,旋转,打开,验证,读取,关闭等等。然后

使用这个类我的自然猜测会是有类似的东西


image = Image()

image.read(" myfile.jpg")

image .rotate()

image.close()



image = Image.open(" myfile.jpg")

image.verify()

image.rotate()

image.close()


也许PIL真正的Image类没有这些方法确实没有这些方法,但是没关系,我的观点是它的工作方式。它是否正常而不是首先创建一个类的实例,它会立即启动它的方法吗?我当然明白,PIL人们只是选择了他们的模块这样工作,

期间,但我只是想知道它是否不会在我的第一个例子中,我更加符合逻辑




我想这只是一个常规问题或程序员感觉怎么样?b $ b喜欢,但是没有这种类型的约定?哪个会更好

pythonic?或者我不应该担心,这完全取决于开发人员?

解决方案

2006年2月1日星期三23:40:37 + 0100,Daniel Nogradi写道:

我对面向对象编程比较新,所以偶尔会对它的用法感到困惑。假设有一个类Image,它有许多方法,旋转,打开,验证,读取,关闭等。然后
使用这个类,我的自然猜测是有类似的东西。 >
image =图片()
image.read(" myfile.jpg")
image.rotate()
image.close()
但现在事实证明,PIL模块使用它作为图像= Image.open(" myfile.jpg")
image.verify()
image.rotate ()
image.close()

PIL的真实Image类可能没有这些方法,但没关系,我的观点是它的工作方式。它是否正常,而不是首先创建一个类的实例,它立即启动它的方法吗?我当然明白,PIL人只是选择他们的模块以这种方式工作,
期间,但我只是想知道它是不是更多逻辑<沿着我的第一个例子的方式来做这件事。

我想这只是一个常规问题或程序员的感觉如何,但没有惯例这个类型?哪个会比较pythonic?或者我不应该担心,这完全取决于开发人员?



你应该看看PIL源文件,open()肯定是一个

class,它在构造时将文件名作为参数。如果你在这些代码中运行
,就没有必要担心,有些东西是传统的,有些是优先考虑的问题。在大多数情况下潜入源码可以帮助澄清你的很多猜测,更好的希望

代码是透明的,我讨厌阅读不可读的代码。 :)


-

聪明人知道他一无所知。


2006年2月1日星期三23:40:37 +0100,Daniel Nogradi写道:

我对面向对象编程比较新,所以搞糊涂了
偶尔使用它。假设有一个类Image,它有许多方法,旋转,打开,验证,读取,关闭等。然后
使用这个类,我的自然猜测是有类似的东西。 >
image =图片()
image.read(" myfile.jpg")
image.rotate()
image.close()
但现在事实证明,PIL模块使用它作为图像= Image.open(" myfile.jpg")
image.verify()
image.rotate ()
image.close()

PIL的真实Image类可能没有这些方法,但没关系,我的观点是它的工作方式。它是否正常,而不是首先创建一个类的实例,它立即启动它的方法吗?我当然明白,PIL人只是选择他们的模块以这种方式工作,
期间,但我只是想知道它是不是更多逻辑<沿着我的第一个例子的方式来做这件事。

我想这只是一个常规问题或程序员的感觉如何,但没有惯例这个类型?哪个会比较pythonic?或者我不应该担心,这完全取决于开发人员?




在这种情况下,Image似乎是一个python模块,具有open函数

定义,PIL的图像不是一个类。


-

聪明人知道他什么都不知道。


Daniel Nogradi写道:

我对面向对象编程比较新,所以搞糊涂了
关于偶尔使用它。假设有一个类Image,它有许多方法,旋转,打开,验证,读取,关闭等。然后
使用这个类,我的自然猜测是有类似的东西。 >
image =图片()
image.read(" myfile.jpg")
image.rotate()
image.close()
但现在事实证明,PIL模块使用它作为图像= Image.open(" myfile.jpg")
image.verify()
image.rotate ()
image.close()

PIL的真实Image类可能没有这些方法,但没关系,我的观点是它的工作方式。它是否正常,而不是首先创建一个类的实例,它立即启动它的方法吗?我当然明白,PIL人只是选择他们的模块以这种方式工作,
期间,但我只是想知道它是不是更多逻辑<沿着我的第一个例子的方式来做这件事。

我想这只是一个常规问题或程序员的感觉如何,但没有惯例这个类型?哪个会比较pythonic?或者我不应该担心,这完全取决于开发人员?



如果我正在编写类似的类,我可能会这样做:


image = Image(" myfile.jpg")

image.rotate()

image.close()


此外,还有一个image.open()方法。在上面,

__init__只在指定文件名时自行调用它。


(当然,我当然不是熟悉PIL模块;

可能会在幕后以他们的方式进行更多的事情。)


-Kirk McDonald


I''m relatively new to object oriented programming, so get confused
about its usage once in a while. Suppose there is a class Image that
has a number of methods, rotate, open, verify, read, close, etc. Then
to use this class my natural guess would be to have something like

image = Image( )
image.read( "myfile.jpg" )
image.rotate( )
image.close( )

But now it turns out that the PIL module uses this as

image = Image.open( "myfile.jpg" )
image.verify( )
image.rotate( )
image.close( )

Perhaps the real Image class of PIL doesn''t have these methods
exactly, but doesn''t matter, my point is the way it works. Is it
normal that instead of first creating an instance of a class, it
starts right away with one its methods? I of course understand that
the PIL people simply made a choice that their module works this way,
period, but I''m just wondering if it wouldn''t have been more "logical"
to do it along the way of my first example.

I guess it''s just a matter of convention or how the programmer feels
like, but there are no conventions of this type? Which would be more
pythonic? Or I shouldn''t worry and it''s totally up to the developer?

解决方案

On Wed, 01 Feb 2006 23:40:37 +0100, Daniel Nogradi wrote:

I''m relatively new to object oriented programming, so get confused
about its usage once in a while. Suppose there is a class Image that
has a number of methods, rotate, open, verify, read, close, etc. Then
to use this class my natural guess would be to have something like

image = Image( )
image.read( "myfile.jpg" )
image.rotate( )
image.close( )

But now it turns out that the PIL module uses this as

image = Image.open( "myfile.jpg" )
image.verify( )
image.rotate( )
image.close( )

Perhaps the real Image class of PIL doesn''t have these methods
exactly, but doesn''t matter, my point is the way it works. Is it
normal that instead of first creating an instance of a class, it
starts right away with one its methods? I of course understand that
the PIL people simply made a choice that their module works this way,
period, but I''m just wondering if it wouldn''t have been more "logical"
to do it along the way of my first example.

I guess it''s just a matter of convention or how the programmer feels
like, but there are no conventions of this type? Which would be more
pythonic? Or I shouldn''t worry and it''s totally up to the developer?


You should take a look at the PIL source files, open() could surely be a
class which takes a filename as an argument at construction time. If you
run across such code, there is really no need to worry, some things are
conventional and some are a matter of preference. In most cases diving
into the source can help clarify a lot of your speculations, better hope
the code is transparent, i hate reading non-readable code. :)

--
A wise man knows he knows nothing.


On Wed, 01 Feb 2006 23:40:37 +0100, Daniel Nogradi wrote:

I''m relatively new to object oriented programming, so get confused
about its usage once in a while. Suppose there is a class Image that
has a number of methods, rotate, open, verify, read, close, etc. Then
to use this class my natural guess would be to have something like

image = Image( )
image.read( "myfile.jpg" )
image.rotate( )
image.close( )

But now it turns out that the PIL module uses this as

image = Image.open( "myfile.jpg" )
image.verify( )
image.rotate( )
image.close( )

Perhaps the real Image class of PIL doesn''t have these methods
exactly, but doesn''t matter, my point is the way it works. Is it
normal that instead of first creating an instance of a class, it
starts right away with one its methods? I of course understand that
the PIL people simply made a choice that their module works this way,
period, but I''m just wondering if it wouldn''t have been more "logical"
to do it along the way of my first example.

I guess it''s just a matter of convention or how the programmer feels
like, but there are no conventions of this type? Which would be more
pythonic? Or I shouldn''t worry and it''s totally up to the developer?



In this case, Image seems to be a python module, with the open function
defined, PIL''s Image is not a class.

--
A wise man knows he knows nothing.


Daniel Nogradi wrote:

I''m relatively new to object oriented programming, so get confused
about its usage once in a while. Suppose there is a class Image that
has a number of methods, rotate, open, verify, read, close, etc. Then
to use this class my natural guess would be to have something like

image = Image( )
image.read( "myfile.jpg" )
image.rotate( )
image.close( )

But now it turns out that the PIL module uses this as

image = Image.open( "myfile.jpg" )
image.verify( )
image.rotate( )
image.close( )

Perhaps the real Image class of PIL doesn''t have these methods
exactly, but doesn''t matter, my point is the way it works. Is it
normal that instead of first creating an instance of a class, it
starts right away with one its methods? I of course understand that
the PIL people simply made a choice that their module works this way,
period, but I''m just wondering if it wouldn''t have been more "logical"
to do it along the way of my first example.

I guess it''s just a matter of convention or how the programmer feels
like, but there are no conventions of this type? Which would be more
pythonic? Or I shouldn''t worry and it''s totally up to the developer?


If I were coding a class like that, I''d probably do it like this:

image = Image("myfile.jpg")
image.rotate()
image.close()

And, in addition, there would be an image.open() method. In the above,
__init__ just calls it on its own when the filename is specified.

(Though, of course, I''m not at all familiar with the PIL module; there
might be more stuff going on behind the scenes with the way they do it.)

-Kirk McDonald


这篇关于OO惯例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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