Python“with” [英] Python "with"

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

问题描述




我正在寻找一种类似于(Turbo)Pascal'的结构,而>

声明。我读到了关于Python的新内容with声明,但是我很失望地知道它做了不同的事情(我仍然没有看到它是什么?b $ b看看它比try.except更好。最后,但那不是我的问题。


是否有类似于我想要的东西'Pythonic足够了?


(如果你不熟悉Pascal,这里是它的工作方式:


with some.big.structure.or.nested.objects.element do begin

member1:= something;

member2:= something;

end;


到底是什么意思?这个等价物:


some.big.structure.or.nested.objects.element.membe r1:= something;

some.big.structure.or .nested.objects.element.membe r2:=某事;


即它是手腕保护程序。我认为C99中存在类似功能,因为

结构。


我知道它几乎总是可以通过使用临时变量来完成:


tmp = some.big.s tructure.or.nested.objects.element

tmp.member1 =东西

tmp.member2 =东西


但是看起来对我很难看。)

-----开始PGP签名-----

版本:GnuPG v1.2.5(MingW32)

评论:将GnuPG与Mozilla一起使用 - http://enigmail.mozdev.org

iD8DBQFG7YzxldnAQVacBcgRAotkAKDeZ / uHf8WWpE + Lpk3ZM56Pnl892wCgvCOn

FDQ2 / IgAA3duJ4R2vz3glpg =

= X0Y +

----- END PGP SIGNATURE -----

Hi,

I''m looking for a construct that''s similar to (Turbo) Pascal''s "with"
statement. I read about the Python''s new "with" statement, but I was
dissapointed to learn that it does something different (I still don''t
see how it''s better than try..except..finally, but that''s not my question).

Is there something similar to what I want that''s Pythonic enough?

(If you''re not familiar with Pascal, here''s how it works:

with some.big.structure.or.nested.objects.element do begin
member1 := something;
member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.membe r1 := something;
some.big.structure.or.nested.objects.element.membe r2 := something;

i.e. it''s a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something

but this looks ugly to me.)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG7YzxldnAQVacBcgRAotkAKDeZ/uHf8WWpE+Lpk3ZM56Pnl892wCgvCOn
FDQ2/IgAA3duJ4R2vz3glpg=
=X0Y+
-----END PGP SIGNATURE-----

推荐答案

Ivan Voras写道:
Ivan Voras wrote:




我正在寻找一个与(Turbo)Pascal'的相似的构造'与'

声明。我读到了关于Python的新内容with声明,但是我很失望地知道它做了不同的事情(我仍然没有看到它是什么?b $ b看看它比try.except更好。最后,但那不是我的问题。


是否有类似于我想要的东西'Pythonic足够了?


(如果你不熟悉Pascal,这里是它的工作方式:


with some.big.structure.or.nested.objects.element do begin

member1:= something;

member2:= something;

end;


到底是什么意思?这个等价物:


some.big.structure.or.nested.objects.element.membe r1:= something;

some.big.structure.or .nested.objects.element.membe r2:=某事;


即它是手腕保护程序。我认为C99中存在类似功能,因为

结构。


我知道它几乎总是可以通过使用一个临时变量来完成:


tmp = some.big .structure.or.nested.objects.element

tmp.member1 =某事

tmp.member2 =某事
Hi,

I''m looking for a construct that''s similar to (Turbo) Pascal''s "with"
statement. I read about the Python''s new "with" statement, but I was
dissapointed to learn that it does something different (I still don''t
see how it''s better than try..except..finally, but that''s not my question).

Is there something similar to what I want that''s Pythonic enough?

(If you''re not familiar with Pascal, here''s how it works:

with some.big.structure.or.nested.objects.element do begin
member1 := something;
member2 := something;
end;

which exactly translates to this equivalent:

some.big.structure.or.nested.objects.element.membe r1 := something;
some.big.structure.or.nested.objects.element.membe r2 := something;

i.e. it''s a wrist-saver. I think a similar feature exists in C99, for
structs.

I know it can be almost always done by using a temporary variable:

tmp = some.big.structure.or.nested.objects.element
tmp.member1 = something
tmp.member2 = something



您可以直接导入成员,使用:

You can import the members directly, using:


>>来自某些人。 big.structure.or.nested.objects.element import member1,
>>from some.big.structure.or.nested.objects.element import member1,



member2


,要求您知道变量名称。但如果您想在导入后执行
,那么您的tmp示例就是您的选择。如果您的变量被命名,那么它将会更少

丑陋它仍然与模块的目的有一些连接




还有其他选项:

member2

which requires you to know the variable names. But if you want to do it
after the import, your tmp example is the way to go. It would be less
ugly if your variable is named so that it still has some connection to
the purpose of the module.

There are other options, too:


>> import some.big。 structure.or.nested.objects.element as tmp
>>import some.big.structure.or.nested.objects.element as tmp



这仍然会让你为变量添加前缀tmp,但是你不要不需要

a单独的声明,专门用于将模块重新绑定到一个新名称。


就个人而言,我没有找到那么难看的短前缀。对我来说
$ b $丑陋和清晰度几乎完全是

代码的同义词,因此保持变量来自何处的确定性

是一件好事。我刚开始工作,我必须维护perl

代码,所以我一直在运行


使用File :: Basename;

my

This will still make you prefix tmp to the variables, but you don''t need
a separate statement devoted to rebinding the module to a new name.

Personally, I don''t find the shorter prefix all that ugly. For me
ugliness and clarity are almost entirely synonymous when it comes to
code, so maintaining the certainty about where the variables came from
is a good thing. I''ve just started a job where I have to maintain perl
code, so I keep running across things like

use File::Basename;
my


dir = dirname(
dir = dirname(


fullpath);


和我一直想知道basename来自哪里。它是内置的吗?它是来自File :: A前缀的
,无论多短,多么晦涩难懂,如何使用b b b b b b b b b that that that that that that that that that that that that that that >
我觉得这很美。所有你需要做的就是查看页面,

,它就在某处。你*可以*用Perl做同样的事情,但实际上这种语言结构不鼓励
。换句话说,

最简单的情况就是做错了。


Python允许你从模块中导入所有名字,没有前缀但是

它会引导你进入与Perl相同的丑陋和命名空间,所以我不会告诉你如何去做。

我认为''import long.name as short''语法将帮助你的手腕最多,但仍保留显式模块引用的清晰度。

你不会有无关的任务(tmp = long.name)

混乱你的代码。


干杯,

Cliff
fullpath);

and I keep wondering where basename came from. Is it a builtin? Was it
from File:: A prefix, no matter how short, how obscure, how
undescriptive, will let you trace exactly where each name came from.
Which I find deeply beautiful. All you have to do is look up the page,
and it''s there somewhere. You *can* do the same with Perl, but it''s
actually discouraged by the structure of the language. In other words,
the simplest case does the wrong thing.

Python does allow you to import all names from a module, unprefixed but
it leads you into the same ugliness and namespace clobbering that you
get with Perl, so I''m not going to tell you how to do it.

I think the ''import long.name as short'' syntax will help your wrists the
most while still retaining the clarity of explicit module referencing.
And you won''t have the extraneous assignment (tmp = long.name)
cluttering up your code.

Cheers,
Cliff


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

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