到目前为止,我的蟒蛇烦恼 [英] My python annoyances so far

查看:74
本文介绍了到目前为止,我的蟒蛇烦恼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好。这些天我正在学习python。我打算用这个帖子

不时发布我对python的烦恼。我希望有人

会向我澄清我误解他们的地方。


烦恼:


1。强调!这有什么用?特别是那些双b / b $ b下划线。我读到的最好的答案是双

下划线表示口译员可以自动使用的特殊方法。例如,4 + 4由解释器扩展为

4 .__添加__(4)。


2.有模块,有功能,和有班级 -

方法!如果一切都是

函数或类方法,那会不会更容易?

解决方案

< blockquote> fl****@gmail.com schrieb:


大家好。这些天我正在学习python。我打算用这个帖子

不时发布我对python的烦恼。我希望有人

会向我澄清我误解他们的地方。


烦恼:


1。强调!这有什么用?特别是那些双b / b $ b下划线。我读到的最好的答案是双

下划线表示口译员可以自动使用的特殊方法。例如,4 + 4由解释器扩展为

4 .__ add __(4)。


http:// docs。 python.org/ref/specialnames.html


2.有模块,有功能,还有类 -

方法!如果一切都是

函数或类方法,那会不会更容易?



我不确定你的意思。模块是模块,它们可以包含

函数,类,顶级代码。函数和类方法类似于
,但有一些区别是有意义的。但是我不确定你是否真的_mean_类方法,因为你混淆了这么多。


python社区是一个非常友好的群体人。然而,你b $ b应该考虑不要把你不理解的东西称为烦恼。


Diez


fl****@gmail.com 写道:


大家好。这些天我正在学习python。我打算用这个帖子

不时发布我对python的烦恼。我希望有人

会向我澄清我误解他们的地方。


烦恼:


1。强调!这有什么用?特别是那些双b / b $ b下划线。我读到的最好的答案是双

下划线表示口译员可以自动使用的特殊方法。例如,4 + 4由解释器扩展为

4 .__添加__(4)。


2.有模块,有功能,和有班级 -

方法!如果一切都是

函数或类方法,那不是更容易吗?



1.下划线。 __add__是一种特殊的方法,当你想要将两个对象加在一起时,它会被调用。 __init__是initalizer方法

,在实例化类时调用它。特殊方法开始

并以双下划线结束。 __variable是一个类(或实例)

变量,不应该在类之外操作,而b / b $ b等于其他语言中的私有变量。我说

有点因为总有一种方法可以获得任何变量,甚至是双下划线的
。 _variable是一个类(或实例)变量

这个类是内部的,通常不应该依赖于
或从外部操纵,除非调用者真的知道什么

他们正在做。


2.模块是代码集合。它们可以是函数或类。

函数是函数,类是类,你需要用

面向对象的语言。仅靠功能是不够的。如果你想要在开始时忽略课程,请这样做。


建议:如果你刚开始请接受有一个

Python与几乎所有传统不同的原因编译

语言。如果你需要一些时间,你会明白并且,如果你最喜欢的话,那就是b $ b,喜欢差异。我一直在创建一个

跨语言COM对象,在VB,Delphi和/或
Python中编写单元测试后,我再一次提醒我为什么喜欢Python。对于某些

测试,我在Delphi版本中有更多行声明,而不是在Python程序中。


-Larry


fl****@gmail.com 写道:


大家好。这些天我正在学习python。我打算用这个帖子

不时发布我对python的烦恼。



请为每个烦恼开始新的主题。过度使用单个线程

对很多人来说是一个烦恼。


我希望有人

将向我澄清我误解他们的地方。



也许替代烦恼与误解获得你的python同行友好的



烦恼:



你的意思是误解:


1.下划线!这有什么用?特别是那些双b / b $ b下划线。我读到的最好的答案是双

下划线表示口译员可以自动使用的特殊方法。例如,4 + 4由解释器扩展为

4 .__ add __(4)。



双下划线可能不是

语言的最美学特征。当你知道如何以及何时使用它们的时候,它们将会完全不打扰你,所以你会从列表中删除#1。


2.有模块,有功能,还有类 -

方法!如果一切都是

函数或类方法,那会不会更容易?



你的意思是未绑定的方法 ;或类方法?除非你进入python的内部,否则它们基本上与函数相同,而b / b $ b意味着你正在寻找烦恼。这样的行为会导致沮丧,并最终导致嫉妒。


所以你应该从你的名单中删除后一点

烦恼......误会。例如:

pyclass Thing(对象):

.... doit = classmethod(doit)

....

pyThing.doit(88)

Param是param。

pyThing.doit(object())

Param是param。

pydef doit(athing,param):

.... print''Param是'%s"。'''%param

....

pyclass事(对象):

.... doit = doit

。 ...

pyt = Thing()

pyt.doit(4)

Param是4。


如果你后来的误解是缺乏实际的类

方法,那么请考虑:

pyclass事情(对象):

.... doit = classmethod(doit)

....

pyThing.doit(4)

Param is" ; 4。

詹姆斯



Hi all. I''m learning python these days. I''m going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What''s the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

2. There are modules, there are functions, and there are classes-
methods! Wouldn''t it have been easier had everything either been a
function or a class method?

解决方案

fl****@gmail.com schrieb:

Hi all. I''m learning python these days. I''m going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What''s the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).


http://docs.python.org/ref/specialnames.html

2. There are modules, there are functions, and there are classes-
methods! Wouldn''t it have been easier had everything either been a
function or a class method?

I''m not sure what you mean here. modules are modules, they can contain
functions, classes, top-level code. Functions and class-methods are
similar, but there are some differences that make sense. But I''m not
sure if you really _mean_ class methods, given that you confuse so much.

The python community is a unusually friendly bunch of people. Yet you
should consider not calling things you don''t understand "annoyances".

Diez


fl****@gmail.com wrote:

Hi all. I''m learning python these days. I''m going to use this thread
to post, from time to time, my annoyances with python. I hope someone
will clarify things to me where I have misunderstood them.

Annoyances:

1. Underscores! What''s the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

2. There are modules, there are functions, and there are classes-
methods! Wouldn''t it have been easier had everything either been a
function or a class method?

1. Underscores. __add__ is a special method that gets called when you
want to add two objects together. __init__ is the initalizer method
that gets called when a class is instantiated. Special methods begin
and end with double underlines. __variable is a class (or instance)
variable that shouldn''t be manipulated outside the class that is
somewhat equivalent to private variables in other languages. I say
somewhat because there is always a way to get to any variable, even
the double underline ones. _variable is a class (or instance) variable
that is internal to the class an normally shouldn''t depended upon
or manipulated from the outside unless the caller really knows what
they are doing.

2. Modules are collections of code. They can be functions or classes.
Functions are functions, classes are classes, you need both in an
object oriented language. Functions alone will not suffice. If you
want to ignore classes at the beginning, please do so.

Suggestion: if you are just starting please accept that there is a
reason for Python being different from almost all "traditional" compiled
languages. If you take some time you will come to understand and,
if you are like most, love the differences. I''ve been creating a
cross-language COM object and after writing unit tests in VB, Delphi, and
Python I am once again reminded why I love Python. I have more lines
of declarations in the Delphi version than in the Python program for some
tests.

-Larry


fl****@gmail.com wrote:

Hi all. I''m learning python these days. I''m going to use this thread
to post, from time to time, my annoyances with python.

Please start a new thread for each annoyance. Overuse of a single thread
is an annoyance to a great many people.

I hope someone
will clarify things to me where I have misunderstood them.

Perhaps substitute "annoyance" with "misunderstanding" to garner the
friendliness of your python peers.

Annoyances:

You mean "Misunderstandings:"

1. Underscores! What''s the deal with that? Especially those double
underscores. The best answer I read on this is that the double
underscores denotes special methods that the interpreter may
automatically use. For example, 4+4 get expanded by the interpreter to
4.__add__(4).

The double underscores are probably not the most aesthetic feature of
the language. By the time you know how and when to use them, they will
not bother you at all and so you will be scratching #1 off your list.

2. There are modules, there are functions, and there are classes-
methods! Wouldn''t it have been easier had everything either been a
function or a class method?

Do you mean "unbound methods" or "class methods"? They are essentially
the same as functions unless you get into the internals of python, which
means you are going out of your way to look for annoyances. Such
behavior will lead to frustration and, eventually, abject ennui.

So you should strike your latter point off your list of
annoyances...misunderstandings. For example:
pyclass Thing(object):
.... doit = classmethod(doit)
....
pyThing.doit(88)
Param is "param".
pyThing.doit(object())
Param is "param".
pydef doit(athing, param):
.... print ''Param is "%s".'' % param
....
pyclass Thing(object):
.... doit = doit
....
pyt = Thing()
pyt.doit(4)
Param is "4".

If your latter misunderstanding is about the lack of actual class
methods, then consider:
pyclass Thing(object):
.... doit = classmethod(doit)
....
pyThing.doit(4)
Param is "4".
James


这篇关于到目前为止,我的蟒蛇烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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