T
T
Timebird2016-06-30 08:35:43
Python
Timebird, 2016-06-30 08:35:43

How to set a variable to a constant?

Hello!
The question is: there is a constant that functions sometimes use in their calculations, looks something like this:

value = 3

def():
#где-то здесь это value применяется
...

def(..):
...

def(...):
#и здесь тоже, например
....

...

It is necessary to make this constant a variable, that is, instead of the value triple, you need to take values ​​when:
the first function call -- value=1;
second function call -- value=2;
third function call -- value = 3;
...
kth function call -- value = k.
How to do it?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-06-30
@Timebird

In the code you provided, you need to add an increment by one after each call to the variable:
Or there is a more complicated way - write a special class that will intercept any call to a certain attribute, return the value of the corresponding variable, and increment it by one.
Or you can also take the count iterator from the itertools standard library and access it via next().
But you are probably better off using the very first method I suggested.

S
Sergey Zelensky, 2016-06-30
@SergeyZelensky-Rostov

and do as you have in the code now, although in this case the variable will be global, which is not very good

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question