Flock behavior using Blender python scripting
Blender actually has a built-in boids implementation. If you don't need to use Python for some reason, you can do this with Particle Systems.


Go to the default cube (or whatever object you want the flock to start from), go to the Particles tab (second from the right in the Properties panel), and click the New button.
Scroll down to the Physics section, and click Boids. There are a lot of options to tweak here.
Now you have a basic flock, but they'll just drift around aimlessly at this point. (You can watch them by pressing Alt+A.) To make them attracted to an object, select the object, click on the Physics tab (all the way to the right), and then click Force Field.
Set it to Force to make them attracted or repelled to the the object.


A negative strength will attract them, while a positive strength will repel them.



In the particle Physics section on the emitter, you can make them interact with other boid flocks in your scene.