避免局部变量声明? [英] Avoiding local variable declarations?

查看:75
本文介绍了避免局部变量声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些旧的Common Lisp函数我想在Python中重写

(我还是Python的新手),我想念的一件事就是不需要

声明局部变量。


例如,我有这个Lisp函数:


(defun random-char()

"从[0-9] [az] [AZ]"

之一生成随机字符(如果(< 50(random 100))

(code-char(+(random 10)48)); ascii 48 = 0

(code-char(+(random 26)(if(< 50(random) 100))65 97)))));

ascii 65 = A,97 = a


我的Python版本如下:


def random_char():

''''''从[0-9] [az]生成一个随机字符[AZ]'''''' '

if random.randrange(0,100)50:

return chr(random.randrange(0,10)+48)#ascii 48 = 0

else:

offset = 65#ascii 65 = A

if random.randrange(0,100)50:

offset = 97#ascii 9 7 = a

返回chr(random.randrange(0,26)+ offset)


从逻辑上讲,它相当于Lisp版本。 br />

但有没有办法避免在

Python版本中使用局部变量(offset)?

解决方案

2008年11月13日星期四下午2:22,dpapathanasiou

< de **************** *@gmail.com写道:


我有一些旧的Common Lisp函数我想用Python重写

(我''我仍然不熟悉Python),我想念的一件事是没有必要

声明局部变量。


例如,我有这个Lisp函数:


(defun random-char()

"从[0-9] [az] [AZ]"
(如果(< 50(随机100))

(code-char(+(random 10)48)); ascii 48 = 0

(code-char(+(随机26)(if(< 50(random 100))65 97)))));

ascii 65 = A,97 = a


我的Python版本如下:


def random_char():

''''''如果random.randrange(0,100)50,则从[0-9] [az] [AZ]'''''生成一个随机字符。



返回chr(random.randrange(0,10)+48)#ascii 48 = 0

else:

offset = 65 #ascii 65 = A

如果random.randrange(0,100)50:

offset = 97#ascii 97 = a

返回chr(随机.randrange(0,26)+ offset)


从逻辑上讲,它相当于Lisp版本。


但是有没有避免在

Python版本中使用局部变量(偏移量)的方法?



任何时候你在语言之间移植,只需

逐字转换代码就不是一个好主意。例如:


导入随机,字符串

def random_char():

返回random.choice(string.ascii_letters + string .digits)


dpapathanasiou写道:


我有一些旧的Common Lisp函数我想要用Python重写

(我还是Python的新手),我想念的一件事是没有必要

声明局部变量。

例如,我有这个Lisp函数:


(defun random-char()

"从其中一个生成一个随机字符[0-9] [az] [AZ]"

(if(< 50(random 100))

(code-char(+(random 10) 48)); ascii 48 = 0

(code-char(+(随机26)(if(< 50(random 100))65 97)))));

ascii 65 = A,97 = a


我的Python版本如下:


def random_char():

''''''从[0-9]中生成一个随机字符] [az] [AZ]''''''

if random.randrange(0,100)50:

return chr(random.randrange(0,10) )+ 48)#ascii 48 = 0

else:

offset = 65#ascii 65 = A

if random.randrange(0, 100)50:

offset = 97#ascii 97 = a

返回chr(random.randrange(0,26)+ offset)


从逻辑上讲,它相当于Lisp版本。


但是有没有办法避免在
$ b中使用局部变量(offset) $ b Python版本?



是的,你可以避免使用偏移,但*为什么*。这肯定不会让你的代码更干净或更容易阅读/理解/维护。


返回chr(random.randrange(0,26) +(97如果random.randrange(0,

100)50其他65)





如果random.randrange(0,100)50:

返回chr(random.randrange(0,26)+ 97)

else:

return chr(random.randrange(0,26)+ 65)





return chr(random.randrange(0,26) + [26,97] [random.randrange(0,

100)50]





...可能还有其他办法可以找到...


你原来的代码有什么问题?

加里赫伦


-
http://mail.python.org/mailman/listinfo/python-list



返回chr(random.randrange(0,26)+(97如果random.randrange(0,

100)50 else 65)





返回chr(random.randrange(0,26)+ [26,97] [random.randrange(0,

100)50]



啊,谢谢,这些是我正在寻找的语法示例。


但原始代码出了什么问题?



我来自一个功能性编程思想学校,你可以在这里尽可能地避免局部变量声明。


I have some old Common Lisp functions I''d like to rewrite in Python
(I''m still new to Python), and one thing I miss is not having to
declare local variables.

For example, I have this Lisp function:

(defun random-char ()
"Generate a random char from one of [0-9][a-z][A-Z]"
(if (< 50 (random 100))
(code-char (+ (random 10) 48)) ; ascii 48 = 0
(code-char (+ (random 26) (if (< 50 (random 100)) 65 97))))) ;
ascii 65 = A, 97 = a

My Python version looks like this:

def random_char ():
''''''Generate a random char from one of [0-9][a-z][A-Z]''''''
if random.randrange(0, 100) 50:
return chr( random.randrange(0, 10) + 48 ) # ascii 48 = 0
else:
offset = 65 # ascii 65 = A
if random.randrange(0, 100) 50:
offset = 97 # ascii 97 = a
return chr( random.randrange(0, 26) + offset )

Logically, it''s equivalent of the Lisp version.

But is there any way to avoid using the local variable (offset) in the
Python version?

解决方案

On Thu, Nov 13, 2008 at 2:22 PM, dpapathanasiou
<de*****************@gmail.comwrote:

I have some old Common Lisp functions I''d like to rewrite in Python
(I''m still new to Python), and one thing I miss is not having to
declare local variables.

For example, I have this Lisp function:

(defun random-char ()
"Generate a random char from one of [0-9][a-z][A-Z]"
(if (< 50 (random 100))
(code-char (+ (random 10) 48)) ; ascii 48 = 0
(code-char (+ (random 26) (if (< 50 (random 100)) 65 97))))) ;
ascii 65 = A, 97 = a

My Python version looks like this:

def random_char ():
''''''Generate a random char from one of [0-9][a-z][A-Z]''''''
if random.randrange(0, 100) 50:
return chr( random.randrange(0, 10) + 48 ) # ascii 48 = 0
else:
offset = 65 # ascii 65 = A
if random.randrange(0, 100) 50:
offset = 97 # ascii 97 = a
return chr( random.randrange(0, 26) + offset )

Logically, it''s equivalent of the Lisp version.

But is there any way to avoid using the local variable (offset) in the
Python version?

Any time you port between languages, it''s rarely a good idea to just
convert code verbatim. For example:

import random, string
def random_char():
return random.choice(string.ascii_letters + string.digits)


dpapathanasiou wrote:

I have some old Common Lisp functions I''d like to rewrite in Python
(I''m still new to Python), and one thing I miss is not having to
declare local variables.

For example, I have this Lisp function:

(defun random-char ()
"Generate a random char from one of [0-9][a-z][A-Z]"
(if (< 50 (random 100))
(code-char (+ (random 10) 48)) ; ascii 48 = 0
(code-char (+ (random 26) (if (< 50 (random 100)) 65 97))))) ;
ascii 65 = A, 97 = a

My Python version looks like this:

def random_char ():
''''''Generate a random char from one of [0-9][a-z][A-Z]''''''
if random.randrange(0, 100) 50:
return chr( random.randrange(0, 10) + 48 ) # ascii 48 = 0
else:
offset = 65 # ascii 65 = A
if random.randrange(0, 100) 50:
offset = 97 # ascii 97 = a
return chr( random.randrange(0, 26) + offset )

Logically, it''s equivalent of the Lisp version.

But is there any way to avoid using the local variable (offset) in the
Python version?

Yes, you can avoid using offset, but *why*. This certainly won''t make
your code cleaner or more easily read/understood/maintainable.

return chr( random.randrange(0, 26) + (97 if random.randrange(0,
100) 50 else 65)

or

if random.randrange(0, 100) 50:
return chr( random.randrange(0, 26) + 97)
else:
return chr( random.randrange(0, 26) + 65)

or

return chr( random.randrange(0, 26) + [26,97][random.randrange(0,
100) 50]

or

... probably other ways can be found ...

but what''s wrong with you original code?
Gary Herron

--
http://mail.python.org/mailman/listinfo/python-list


return chr( random.randrange(0, 26) + (97 if random.randrange(0,
100) 50 else 65)

or

return chr( random.randrange(0, 26) + [26,97][random.randrange(0,
100) 50]

Ah, thanks, these are the syntax examples I was looking for.

but what''s wrong with you original code?

I come from a functional programming school of thought, where you
avoid local variable declarations if at all possible.


这篇关于避免局部变量声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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