简体   繁体   中英

“object has no attribute” even though it is defined in constructor and instance of an object has been created

There have been similar questions, but none of them had answer I was looking for. I am getting: AttributeError: 'ParticleFilter' object has no attribute 'initialCoordinates' (function create_gaussian_particles ) even though I have definted in in init constructor and have created an instance of it, I can't sure what is the problem, I tried to look up myself, but nothing, here is the code:

import numpy as np

class ParticleFilter(object):

    def __init__(self, N, initialConditions, imageSize):

        self.create_particles(N, initialConditions, imageSize)
        self.create_weights(N)
        self.imageSize = imageSize
        self.initialCoordinates = np.array([initialConditions[0, 0], initialConditions[0, 1]])
        self.initialStdDev = np.array([initialConditions[1, 0], initialConditions[1, 1]])

    def create_particles(self, N, initialConditions, imageSize):
        if initialConditions is None:
            self.create_uniform_particles(N, imageSize)
        else:
            self.create_gaussian_particles(N, initialConditions)

    def create_gaussian_particles(self, N, initialConditions):
        self.particles = np.empty((N, 2))
        self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0])
        self.particles[:, 1] = self.initialCoordinates[1] + (np.random.randn(N) * self.initialStdDev[1])

    def create_uniform_particles(self, N, imageSize):
        pass

    def create_weights(self, N):
        pass

N = 1000
initialPosition = np.array([10, 10])
initialStdDev = np.array([5, 5])
initialConditions = np.array([initialPosition, initialStdDev])
imageSize = np.array([480, 360])
pf = ParticleFilter(N, initialConditions, imageSize)

File "c:/Projects/pf2.py", line 65, in <module>
    pf = ParticleFilter(N, initialConditions, imageSize)

File "c://Projects/pf2.py", line 8, in __init__
    self.create_particles(N, initialConditions, imageSize)

File "c://Projects/pf2.py", line 18, in create_particles
    self.create_gaussian_particles(N, initialConditions)


 File "c://Projects/pf2.py", line 22, in create_gaussian_particles
    self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0])

AttributeError: 'ParticleFilter' object has no attribute 'initialCoordinates'

A couple of things here - first, __ init __ is not a constructor . That may seem pedantic, but it can be important not to think of it like one. Secondly, if you look at the stack trace that is produced it will tell you the exact sequence of events that occurred to cause the error. Stack traces are super helpful, not just a bunch of noise the interpreter makes when something goes wrong. They should be the first thing you look when an exception occurs. In this case, you can see that the line that causes the error:

self.particles[:, 0] = self.initialCoordinates[0] + (np.random.randn(N) * self.initialStdDev[0])

Which is in the method create_gaussian_particles(self, N, initialConditions) . Looking at the stack frame before that, we can see that create_gaussian_particles(...) is called from __init__(...) before initialCoordinates is defined . The code is, in a very roundabout way, attempting to access the property before it is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM