Flat shaded mesh - missing separate colours/normals for connected vertices?

I recently got started with python scripting, so I am still very new to this!

I am trying to set the vertex color of vertices looking'up' to white, otherwise set it to black.

I use a dot product to determine whether a vertex is pointing upwards or not (vertex.normal.dot(Vector((0,0,1))) > 0.9)

I run my script on the default cube, and it doesn't work correctly. I noticed that even though I have flat shading enabled, the vertices are all connected and 'share' a normal and color. For this reason, I can't make the 4 vertices that define the top of the cube white and the vertices that are a part of the sides of the cube black. They share the information. Here is what I get when I run my script:

If I separate the top faces and run the script again, the colouring is correct as expected. But whats strange is that if I remove duplicates to reattach the vertices back together again, the colour information is retained! This is what I expected it to do in the first place. Here is a screenshot. In the left image, the faces are still separated - you can see that the upwards facing faces have their own normals pointing straight up, as they are not attached to the rest of the mesh. In the right image, I have run the 'remove duplicates' command to merge everything back together. The normals on the corners now look like they are merged again, but the colour information is retained.

I was wondering how I can define this extra colour information via script, without having to detach the faces first (which defeats the purpose of the script)

You’ll want to use “split normals”.

1. Set your faces to 'smooth' shading, and enable use_auto_smooth mesh option).
2. Compute the normals (mesh.calc_normals_split()).
3. Access them from loops (mesh.loops[0].normal).
4. Don’t forget to free them once you do not need them anymore (mesh.free_normals_split()).