0

If I have a simple object like:

class Toy(object):

    def __init__(self):
        self.flag = 1

If I make 100 unique copies:

import numpy as np
from copy import deepcopy

t = Toy()
toys = np.array([deepcopy(t) for x in range(100)])

Is it possible to do a numpy operation on this that will change the flag like the following?

from random import randint
def deflag(x):
    x.flag = randint(0,9)
    return x

If I do a map operation like it works but it's not vectorized as far as I can tell.

deflagged = map(deflag, toys)
minimumnz
  • 35
  • 1
  • 6
  • 1
    If you are using a class to just keep a bunch of attributes together (as `deflag` is function and not a method), I'd suggest to use a structured array instead. –  May 29 '17 at 02:08
  • 1
    https://stackoverflow.com/questions/43575926/returning-a-vector-of-class-elements-in-numpy/43576406#43576406 explores creating objects like this and accessing attributes. But that's not the same as modifying existing objects. Often an object array is little more than a glorified or debased list. – hpaulj May 29 '17 at 03:49

0 Answers0