Avoiding local variable declarations?

dpapathanasiou denis.papathanasiou at gmail.com
Thu Nov 13 15:22:02 EST 2008


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?



More information about the Python-list mailing list