2

Variable a doesn't exist

I can write in Lua language:

a = (a or 0) + 1

Now a = 1

this allows me not to declare the variable "a" in advance. This is analog of

a = 0
a = a + 1

How can I do the same in Python?

a = (a or 0) + 1

P.S. Why is it important? To avoid assign zero to variables:

python (4 lines of code):

for ticker in ticker_list:
    total_volume[ticker] = 0

for a in range (1,10):
    total_volume[ticker] = total_volume[ticker] + a

lua (2 lines of code):

for a=1,9 do:
    total_volume[ticker] = (total_volume[ticker] or 0) + a
6
  • I suppose a = (None or 0) + 1would do what you want, but I cannot see a use-case. Commented Apr 11, 2020 at 10:09
  • 1
    What's the benefit of doing that rather than your first approach of defining a=0 ? Commented Apr 11, 2020 at 10:09
  • @DeveshKumarSingh dear friend it saves many lines of code Commented Apr 11, 2020 at 10:11
  • 1
    @LydiavanDyke I've edited question to answer You Commented Apr 11, 2020 at 10:46
  • 1
    @IgorK.: "dear friend it saves many lines of code" It saves exactly one line of code. You could easily declare a local variable in the loop which contains either the value or 0 if the value were none, then add one to the local variable. Commented Apr 11, 2020 at 17:10

2 Answers 2

1

Python does have a ternary operator, like Lua and many other languages

But unlike Lua, Python does not handle an undefined variable as a "false" value, it will throw an error if you attempt to evaluate an undefined variable. Specifically in your case you will get a key error for the key ticker not being present.

Lua Ternary:

    total_volume[ticker] = (total_volume[ticker] or 0) + a

Python Equivalent:

    total_volume[ticker] = (total_volume[ticker] if ticker in total_volume else 0) + a

Where Lua will pass you the value before the or if it is truthy, python will pass you the value before the if when the statement that follows is true, and the value after the else when it is false.

To safely evaluate if the key was in the dictionary we can use in.

This is mostly to demonstrate the ternary operation you were using in Lua and how to do it in Python, but that doesn't mean it is the right tool for this problem.


A cleaner solution I would suggest:

   total_volume[ticker] = total_volume.get(ticker, 0) + 1

It is less code and easier to reason about. get will return total_volume[ticker] or if the key is not present in the dictionary it will return the default value, 0.

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

1 Comment

Thank You! Which of them is faster? It is important for me because I use it in millions lines of datafiles.
1

For your use case, I would suggest to use defaultdict:

from collections import defaultdict

d = defaultdict(int)
d["counter"] += 5
print(d.items()) # output: dict_items([('counter', 5)])

BUT -

I don't think it's a good practice but you can use locals() or globals() to access variables dynamically in the desired scope:

a = 1
print(locals().get("a")) # ouput:1

c = locals().get("c", 2) + 1 
print(c) # output 3

1 Comment

There is also the possibility of doing it with a standard dict: d["counter"] = d.get("counter", 0) + 5

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.