0

How can I do this in python?

a = 0
func(a+=1) # a = 1
func(a+=1) # a = 2

Now I have had to solve it like this:

a = 0
a+=1
func(a)
a+=1
func(a)
...

and there must be a better way, right?

Edit: Actually I also want to be able to pass it to different functions:

a = 0
a+=1
func1(a)
a+=1
func2(a)
a+=1
func1(a)
...
martineau
  • 119,623
  • 25
  • 170
  • 301
Johanna
  • 39
  • 4
  • `res = [func(a) for a in range(n)]` ? – jpp Apr 12 '18 at 16:36
  • 1
    I don't see anything wrong with your solution. – Scott Hunter Apr 12 '18 at 16:37
  • Possible duplicate of [Behaviour of increment and decrement operators in Python](https://stackoverflow.com/questions/1485841/behaviour-of-increment-and-decrement-operators-in-python) – EtienneG Apr 12 '18 at 16:37
  • @jpp thanks, that worked but I realised I needed it to work for different functions – Johanna Apr 12 '18 at 16:39
  • @ScottHunter I mean, writin a+=1 works, but I am calling like 30 functions in a test, and I would like it to be a bit smoother, by increasing the variable in the function call... – Johanna Apr 12 '18 at 16:40
  • Ok, so define a function to return a function: `def switcher(a): return f(a); res = [switcher(a)(a) for a in range(n)]` where `f` is your logic. Your `switcher` can even be a dictionary and act like a dispatcher. – jpp Apr 12 '18 at 16:40
  • I updated my answer to decorate the functions as an alternative, inspired by @jpp – progmatico Apr 12 '18 at 17:18

4 Answers4

3

your solution is okay, but if you want to have a counting side effect, use itertools.count object:

import itertools

def f(x):
    print(x)

c = itertools.count()
f(next(c))
f(next(c))

or the variant, performing the next call in the functions themselves (itertools.counter is mutable so you can modify it inside the function):

import itertools

def f(c):
    x = next(c)
    print(x)

c = itertools.count()
f(c)
f(c)

you can initialize it to a non-zero value: c = itertools.count(3)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

Something simple like this:

a = 0
a+=1;func1(a)
a+=1;func2(a)

Each line is only 2 characters longer than your original request.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • I hadn't event thought about that I could to the increase and function call in the same line! Thanks for your answer, it helped a lot! :) – Johanna Apr 13 '18 at 07:36
0

There is no way in the sense that in Python, assignment is an instruction, not an operator whose operation returns a value.

You'll have to define a function and either use the global keyword with a or turn a into a mutable. You can also use a decorator for your functions.

a = [0]
#a = 0 # alternative version

def inc(x):
    x[0] += 1
    return x[0]

#    global a # alternative version
#    a += 1
#    return a


def f1(x):
    return x + 1

def f2(x):
    return x + 2


# or (inspired by @jpp comment) decorate your functions
def inc1(x):
    def inner(x):
        x[0] += 1
        return x[0]
    return inner

@inc1
def f3(x):
    return x


for i in range(10):
    print(inc(a))

print()
print(f1(inc(a)))
print(f2(inc(a)))

print()

a = [0]
print(f3(a))
print(f3(a))
progmatico
  • 4,714
  • 1
  • 16
  • 27
0

You can have a wrapping function that calls your function f and simultaneously increment the value of j!

>>> def f(a):print("Value of a is: %d"%a)
...
>>> c=lambda i:(i+1,f(i+1))
>>> j=0
>>> j,_=c(j)
Value of a is: 1
>>> j
1
>>> j,_=c(j)
Value of a is: 2
>>> j
2
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23