Re:理解pythonic方式:为什么a.x = 1优于a.setX(1)? [英] Re: Understanding the pythonic way: why a.x = 1 is better thana.setX(1) ?

查看:60
本文介绍了Re:理解pythonic方式:为什么a.x = 1优于a.setX(1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起......按下输入但是真的不想。


正如我所说,让我们说我有一个班级

A类:

def __init __(自我):

self.x =无


Python使决定允许开发人员直接访问

属性x,这样他们就可以直接写:ax = 1,或者

无论如何;这对我来说有一个不幸的副作用,如果我写,

例如a.y = 1,当我真的想要写a.x = 1时没有人

关心它,直到以后我才能发现这个错误。


当然,我知道我在做什么新鲜,我非常了解

代码,以及其他任何东西,我将能够避免这种愚蠢的错误;

然而,我很害怕当我累了的时候,我必须把我的

手放在别人的代码上,依此类推。


请,明白我不是说python是错的......在

之后所有,如果它错了,我可以转向Java这样的语言,它有一个

不同的方法。我真的非常有兴趣阅读它上面的

讨论,如果它们可用的话。


问候

Marco


2008年9月4日星期四下午12:57,Marco Bizzarri

< ma ************ @ gmail.comwrote:


让我们说我有一个班级,在哪里我可以写:


-

Marco Bizzarri
http://notenotturne.blogspot .com /
http://iliveinpisa.blogspot.com/




-

Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/

解决方案

当然,我知道虽然我很新鲜,但我非常了解


代码,以及其他任何东西,我将能够避免这种愚蠢的错误;

然而,我很害怕我累了的时候,当我不得不把我的

手放在代码上别人,等等。


请理解我不是说python错了......

all,如果是错了,我可以转向像Java这样的语言,它有一个不同的方法。如果它们可用,我真的很有兴趣阅读它上面的讨论。



这与setter和getter的问题没什么关系。因为在

你的代码你可以写的


def setX(self,x):

self.y = x


def getX(个体经营):

返回self.x


因此错误的发生方式相同。


你基本上要问的是:为什么python是动态的而不是静态的?


因为*那*是有区别的。在静态语言中,您需要首先声明您希望获得的内容,例如:哪些变量和

方法是可用的等等。


当然那么编译器(或任何评估静态声明的人)

如果你尝试像你描述的那样噱头,你可以呕吐。


Python(以及其他语言,如Ruby,Javascript)选择其他。他们给你

随意添加实例变量(甚至是方法),

偶然出现错误的成本如上所述。


OTOH,Java让我写了更多的代码,让我变得疲惫和

做了一些愚蠢的错误(并且有很多可用的静态

语言也是如此,否则没有C / C ++或Java程序会崩溃....)

变得更有可能...... YMMV。


Diez


Marco Bizzarriaécrit:


抱歉...按下输入但是真的没有想要。


正如我所说,让我们说我有一个班级


A级:

def __init __(self):

self.x =无


Python决定允许开发人员直接访问

属性x,



如果你公开你的属性那么Java (鉴于Java缺乏对计算属性的支持,这将是一个很大的

错误,但这是另一个问题。


这样他们就可以直接写:ax = 1或

无论如何;这对我来说有一个不幸的副作用,如果我写,

例如a.y = 1,当我真的想要写a.x = 1时没有人

关心它,



我假设*你*做! - )


但是这与显式的setter或者没有关系几乎没有 - 它是因为默认的[1] Python'的对象行为是

支持任意的事实所以
属性设置。


[1]有些对象不是,但这主要是出于优化原因。


和直到以后我才能发现这个错误。



不确定,但像pylint或pychecker这样的IIRC工具可能会警告你这个问题。但无论如何:


当然,我知道,虽然我很新鲜,但我非常了解

代码等等,我将能够避免这种愚蠢的错误;

但是,我很害怕我累了的时候,当我不得不把我的

交给别人的代码,依此类推。



Python中的编辑/测试周期通常足够快,所以你应该快速发现问题*漂亮*。这至少是我从8年以上学习的python(以及其他一些动态语言)编程中学到的...


不是说问题就像你提到的那个(或类似的问题

动态打字等)从来没有发生过,也不是他们永远不会痛苦

来追踪和修复 - 只是他们发生的方式少于一个人b / b可能会担心,并且大部分时间都很快被发现和修复。


请理解我'我没有说python是错的......在

之后,如果它错了,我可以转向Java这样的语言,它有一个不同的方法。在上面。



我不认为这是正确的问题。或错误的 - 主要是

权衡和平衡。但是如果你去静态打字并且(据称)

可证明正确,你可能想看看像OCaml这样的语言。


如果它们可用,我真的很有兴趣阅读它上面的讨论。



嗯......大多数这些讨论都是可归结为束缚和纪律

proponants断言 - 反对所有证据 - 动态语言

是不安全的,除了简单的丢弃脚本之外的其他任何东西都无法使用

或玩具项目,以及动态支持者争论 - 反对所有证据

- 静态打字和一切相关只是浪费时间

(FWIW,你可能发现自己在第一个营地直到大约2k年和

第二个五年或者六年后的第二个)。有时候,有人会更加明智地尝试采取更均衡的方法解决

问题,通常无济于事。

2008年9月4日星期四下午1:19,Diez B. Roggisch< de *** @ nospam.web.dewrote:


>

你基本上要问的是:为什么python是动态的而不是静态的?



最有可能你是对的。也许我会回到我的

大学书籍并再看一遍:-)


谢谢

Marco


-

Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/


Sorry... pressed enter but really didn''t want to.

As I said, let''s say I have a class

class A:
def __init__(self):
self.x = None

Python makes the decision to allow the developers to directly access
the attribute "x", so that they can directly write: "a.x = 1", or
whatever; this has for me the unfortunate side effect that if I write,
for example "a.y = 1", when I really wanted to write "a.x = 1" no one
cares about it, and I''m unable to spot this error until later.

Of course, I know that while I''m fresh, I''ve a good knowledge of the
code, and anything else, I will be able to avoid such stupid errors;
however, I''m afraid of the times when I''m tired, when I have to put my
hands on the code of someone else, and so on.

Please, understand that I''m not stating that python is wrong... after
all, if it is wrong, I can move to a language like Java, which has a
different approach on it. I''m really very interested in reading past
discussion on it, if they are available.

Regards
Marco

On Thu, Sep 4, 2008 at 12:57 PM, Marco Bizzarri
<ma************@gmail.comwrote:

Let''s say I''ve a class a, where I can write:

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/



--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/

解决方案

Of course, I know that while I''m fresh, I''ve a good knowledge of the

code, and anything else, I will be able to avoid such stupid errors;
however, I''m afraid of the times when I''m tired, when I have to put my
hands on the code of someone else, and so on.

Please, understand that I''m not stating that python is wrong... after
all, if it is wrong, I can move to a language like Java, which has a
different approach on it. I''m really very interested in reading past
discussion on it, if they are available.

This has not much to do with the question of setters and getters. Because in
your code you could write

def setX(self, x):
self.y = x

def getX(self):
return self.x

So the error occurs in the same way.

What you are essentially asking is: why is python dynamic instead of static?

Because *that* is what is the difference. In a static language, you need to
declare first what you want to be available, e.g. which variables and
methods are available and such.

Of course then the compiler (or whoever evaluates the static declarations)
can puke on you if you attempt a stunt like the one you describe.

Python (and other languages such as Ruby, Javascript) chose other. They give
you the freedom to add instance variables (or even methods) at will, with
the cost of the occasional error like the above.

OTOH, Java makes me write soooo much more code that me becoming tired and
doing some stupid mistake (and there are lots of them doable in static
languages as well, otherwise no C/C++ or Java-program would crash....)
becomes much more likely....YMMV.

Diez


Marco Bizzarri a écrit :

Sorry... pressed enter but really didn''t want to.

As I said, let''s say I have a class

class A:
def __init__(self):
self.x = None

Python makes the decision to allow the developers to directly access
the attribute "x",

So do Java, if you make your attribute public (which would be a big
error given Java''s lack of support for computed attribute, but this is
another problem).

so that they can directly write: "a.x = 1", or
whatever; this has for me the unfortunate side effect that if I write,
for example "a.y = 1", when I really wanted to write "a.x = 1" no one
cares about it,

I assume *you* do !-)

But this is barely related to having explicit setters or not - it comes
from the fact that the default[1] Python''s object behaviour is to
support arbitrary attribute setting.

[1] some objects don''t, but this is mostly for optimization reasons.

and I''m unable to spot this error until later.

Not sure, but IIRC tools like pylint or pychecker might be able to warn
you about this. But anyway :

Of course, I know that while I''m fresh, I''ve a good knowledge of the
code, and anything else, I will be able to avoid such stupid errors;
however, I''m afraid of the times when I''m tired, when I have to put my
hands on the code of someone else, and so on.

The edit/test cycle in Python is usually fast enough so you should spot
the problem *pretty* quickly. This is at least what I learned from 8+
years of python (and a couple other dynamic languages) programming...

Not to say that problems like the one you mention (or similar problems
with dynamic typing etc) never happens, nor that they''re never painful
to track down and fix - just that they happen way less often than one
might fear, and are most of the time really quickly spotted and fixed.

Please, understand that I''m not stating that python is wrong... after
all, if it is wrong, I can move to a language like Java, which has a
different approach on it.

I don''t think it''s a matter of "right" or "wrong" - mostly a matter of
tradeoffs and balance. But if you go for static typing and (allegedly)
provable correctness, you may want to have a look at languages like OCaml.

I''m really very interested in reading past
discussion on it, if they are available.

Well... Most of these "discussions" alas boil down to bondage&discipline
proponants asserting - against all evidences - that dynamic languages
are unsafe and unusable for anything else than simple throw-away scripts
or toy projects, and dynamic proponants arguing - against all evidences
- that static typing and everything related is just a waste of time
(FWIW, you might find myself in the first camp until approx year 2k and
the second for the five or six following years). And sometimes, someone
a bit more sensible trying to take a more balanced approach to the
problem, usually to no avail.


On Thu, Sep 4, 2008 at 1:19 PM, Diez B. Roggisch <de***@nospam.web.dewrote:

>
What you are essentially asking is: why is python dynamic instead of static?

Most probably you''re right. Maybe I will make a trip back to my
university books and take a look at them again :-)

Thanks
Marco

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/


这篇关于Re:理解pythonic方式:为什么a.x = 1优于a.setX(1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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