They most likely do this because they're vulnerable when they are opaque, and spend a lot of time in one position.

Shedding is a 'stinky' process (which is why male snakes go nuts over newly shed females). Since they're making a big stink then anyhow, they also get rid of all the stored up feces, and they can leave the whole predator-attracting mess behind.