I have the following Bird definition:
class Bird:
def __init__(self, swarm, position = None):
if (swarm == None):
raise ValueError("swarm variable should not be None!")
if (not(type(swarm)).__name__ == 'ParticleSwarmOptimization'):
raise TypeError("swarm variable must be of type ParticleSwarmOptimization!")
It is raising error in the last line. In the interpreter it prints:
(type(swarm)).__name__
'instance'
I’d expect it to print “ParticleSwarmOptimization”.
I’m calling Bird’s constructor the following way:
def AddBird(self, position = None):
self.birds += Bird(self, position)
With this I want that every bird has a reference to the main ParticleSwarmOptimization class, and I want to ensure that every time a Bird is created I have in fact a ParticleSwarmOptimization instance reference and not anything else.
Thanks!
use:
The culture of Python is to not do these sorts of defensive checks, instead to simply use the variable. If it is of the wrong type, an exception will eventually be raised.