一些“pythonic”是指“pythonic”。对Python的建议 [英] Some "pythonic" suggestions for Python

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

问题描述

我喜欢Python,它是我最喜欢的两种

语言之一。我建议Python窃取一些

方面的S语言。


----------------- --------------------------------------

1.目前在Python

def foo(x,y):...

将名称foo赋给函数对象。这是pythonic吗?

为什么不像大多数其他作业一样使用=运算符?

将函数对象定义为函数,让用户将它们放在哪里

他们想要。摆脱lambda,摆脱def,只使用=

进行分配。


foo = function(x,y)x + y * 2#示例S语言代码

bar = foo

bar(3,4)

m = lapply(s,foo)

bb = lapply(s,function(t)t [3] * 4)


foo = func(x,y):x + y * 2#可能的python代码

bar = foo

bar(3,4)

m = barf(s,foo)

bb = barf (s,func(t):t [3] * 4)


----------------------- --------------------------------

2.允许序列为索引:


>> s = [" hello",6,33," none" ]
x = [1,3]
[s [y] for y in x]#当前详细版本



[6,''none'']


> ;> s [x]#更简单,更清晰,更高效



http://www.thescripts.com/forum/thread22741.html

"虽然我们在这里,但我也不明白为什么序列不能用作指数的b $ b。为什么不,比如l [[2,3]]或l [(2,3)]?为什么特殊的

切片概念? "难道不是unpythonic吗?


------------------------------- -------------------------

3.当我第一次开始使用python时,我经常使用

地图,因为我不想学习列表推导的额外语法,其中
看起来非常非结构化。


#这是可读的吗?

b = [x + y代表vec1中的x如果x> 0代表vec2中的y,如果y> x]


也许列表理解语法更像是python的其余部分。 "对于"可以返回一个由继续给出的列表

参数:


b = for vec1中的x:

if(x> 0):继续#" return"没有

继续为vec2中的y:

if(x> y):continue(x + y)


注意我的代码实际上会返回一个列表列表

而不是像列表理解那样的单个列表。

更有条理的语法打开了大门的大门

更复杂,但仍然可理解(因此更多

pythonic),列表理解。


------------- -------------------------------------------

我知道这些想法并不完美,但我认为他们

可能会更好......消防。


-Frank

解决方案

11月8日中午12点,Frank Samuelson

< newsdump0711.12.cud ... @ neverbox .comwrote:


def foo(x,y):...

将名称foo赋给函数对象。


为什么不像大多数其他作业一样使用=运算符?



FWIW,它还绑定__name__属性:


foo = lambda(x,y):...

foo .__ name__ =''foo''

雷蒙德


11月8日,3: 00 pm,Frank Samuelson

< newsdump0711.12.cud ... @ neverbox.comwrote:


我知道这些想法不是完美,但我认为他们

可能会更好......消防。



Python不是Lisp。

Carl Banks


< blockquote> Frank Samuelson pisze:


foo = function(x,y)x + y * 2#示例S语言代码



丑陋。


-

Jarek Zgoda
http://zgodowie.org/


I love Python, and it is one of my 2 favorite
languages. I would suggest that Python steal some
aspects of the S language.

-------------------------------------------------------
1. Currently in Python
def foo(x,y): ...
assigns the name foo to a function object. Is this pythonic?

Why not use the = operator like most other assignments?
Define function objects as "function"s, let users put them where
they want to. Get rid of lambda, get rid of def, only use =
for assignments.

foo = function(x,y) x+y*2 # Example S language code
bar = foo
bar(3,4)
m = lapply( s, foo )
bb = lapply(s, function(t) t[3]*4 )

foo = func(x,y): x+y*2 # Possible python code
bar = foo
bar(3,4)
m = barf( s, foo )
bb = barf(s, func(t): t[3]*4 )

-------------------------------------------------------
2. Allow sequences to be indices:

>>s=["hello", 6, 33, "none"]
x= [1,3]
[ s[y] for y in x] # Current verbose version

[6, ''none'']

>>s[x] # Simpler, clearer, more productive

To quote a poster at http://www.thescripts.com/forum/thread22741.html,
"While we are at it, I also don''t understand why sequences can''t be
used as indices. Why not, say, l[[2,3]] or l[(2, 3)]? Why a special
slice concept? " Isn''t that unpythonic?

--------------------------------------------------------
3. When I first started using python, I frequently used
map, because I didn''t want to have to learn the
additional syntax of list comprehensions, which
appeared very nonstructured.

# Is this readable?
b= [x+y for x in vec1 if x>0 for y in vec2 if y>x ]

Perhaps a list comprehension syntax more like the rest
of python. "for" could return a list given by continue
arguments:

b= for x in vec1 :
if (x>0): continue # "returns" nothing
continue for y in vec2:
if (x>y): continue(x+y)

Note that my code would actually return a list of lists
rather than a single list like the list comprehension.
More structured syntax opens the door to having much
more complicated, yet still comprehensible (thus more
pythonic), list comprehensions.

--------------------------------------------------------

I know these ideas are not perfect, but I think they
may be better... Fire away.

-Frank

解决方案

On Nov 8, 12:00 pm, Frank Samuelson
<newsdump0711.12.cud...@neverbox.comwrote:

def foo(x,y): ...
assigns the name foo to a function object.

Why not use the = operator like most other assignments?

FWIW, the also binds the __name__ attribute:

foo = lambda(x,y): ...
foo.__name__ = ''foo''
Raymond


On Nov 8, 3:00 pm, Frank Samuelson
<newsdump0711.12.cud...@neverbox.comwrote:

I know these ideas are not perfect, but I think they
may be better... Fire away.


Python isn''t Lisp.
Carl Banks


Frank Samuelson pisze:

foo = function(x,y) x+y*2 # Example S language code

Ugly.

--
Jarek Zgoda
http://zgodowie.org/


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

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