dry run of nump's vectorize

38 views Asked by At

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:

  1. Does anybody now the purpose of this dry run? For me I just seems to be a source of potential trouble.

  2. What is the pythonic way to deal with a problem, illustrated by the example?

1

There are 1 answers

2
hpaulj On

from the docs

The data type of the output of vectorized is determined by calling the function with the first element of the input. This can be avoided by specifying the otypes argument.

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.