When I run a function, which is vectorized with numpy it is always executed once more than I would expect. Thus, before the actual calling starts, there seems to be a dry run. Recently, I run into trouble because of this. See the following minimal example:
import numpy as np
class PERSON:
def __init__(self, age):
self.age = age
class TIME:
def __init__(self):
self.ages = np.array([0,0])
def init_persons(self):
vec_init_persons = np.vectorize(self.__scalar_init_person)
self.persons = vec_init_persons(self.ages)
def __scalar_init_person(self, age):
return PERSON(age)
def let_time_pass(self):
vec_let_time_pass = np.vectorize(self.__scalar_let_time_pass)
vec_let_time_pass(self.persons)
def __scalar_let_time_pass(self, person):
person.age += 1
time = TIME()
time.init_persons()
time.let_time_pass()
print("Age of person 1: {}".format(time.persons[0].age)) # output is 2 not 1!
print("Age of person 2: {}".format(time.persons[1].age)) # output is 1
Normally, I would have guessed, the age of both persons is 1. So my questions are:
Does anybody now the purpose of this dry run? For me I just seems to be a source of potential trouble.
What is the pythonic way to deal with a problem, illustrated by the example?
from the docs