Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I create a new object for each class instance?

Please look at the code snippet below, I asked the question below

class SAMPLES:
    x = np.zeros(10)

    def __init__(self, k, value):
        self.x[k] = value


a = SAMPLES(0, 9)
b = SAMPLES(0, 10)
print(a.x[0])
print(b.x[0])

OUTPUT:

10
10

But the output must be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

9
10

How should I solve this problem?

>Solution :

Declare x within the __init__ method.

class SAMPLES:
    def __init__(self, k, value):
        self.x = np.zeros(10)
        self.x[k] = value


a = SAMPLES(0, 9)
b = SAMPLES(0, 10)
print(a.x[0])
print(b.x[0])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading