0

I searched around but I did not found the answer to this question.

Is there any aesthetic, pythonic way to create temporary variables in an arbitrary scope so I am sure they won't ever appear outside this scope ?

For exemple in OCaml, I can write :

let x = 3 and y = 5 in z = x + y

I'd love to do some stuff like this in Python. For exemple, let's imagine I create a module talker.py with a lot of useful functions, and each of them uses a variable that I don't want to copy again and again.

# talker.py

sentance = ( 'Has provinciae has dictione emergunt sorte aptae'
  'iuris navigerum nusquam iuris in natura sorte aptae visitur'
  'in verum dictione flumen.' )

def talk() :
  print sentance

def talk_loudly() :
  print sentance.upper()

def any_other_useful_function :
  # do stuff using sentance

Then I want to use all of these wonderful functions in another file, but :

  • I want to import all these functions at once (without naming each of them)
  • I don't want to have access to sentance : this variable has done it's job, now I want it to fall in darkness and to be forgotten.

But if I call this module with import talk or from talk import *, this second condition won't be respected.

This is an example of where I could use a "special scope for temporary variables", even if it is not the only situation in which I wish I had this at range.

I thought about using the with ... : statement but I wasn't satisfied with the result.

Any idea is welcomed, but I am looking for the most aesthetic and least wordy manner to proceed.

2
  • Object orient your whole program, Don't stick to non OO methodology Commented May 20, 2016 at 15:25
  • @sameerasy Doing so, I'd have to keep a talker_instance.* syntax everywhere whereas I prefer to see it as a repertory of utility functions I want to have access everywhere in the program as if I wrote them just above. Commented May 20, 2016 at 15:30

2 Answers 2

2

You can define the __all__ name on your module, to control the names which will be available when you are using from talk import *.

Documented here.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway to tweak this solution in order to have the same behaviour with import talker (i.e. talker.talk() is recognized but not talker.sentance) ?
No, not that I know of.
1

Firstly, sentance isn't temporary. Those functions are relying on its existence. You could del sentance and it would be gone but the functions would throw an error.

A common Python convention is to prefix the name with an underscore. This signals that the variable is 'protected'. It doesn't do anything, but programmers know that they probably shouldn't access it from outside the module (or class).

1 Comment

Note: a leading underscore will also prevent it from getting collected by import *, without needing to specify that explicitly using __all__

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.