Some companies have a strong safety culture. The importance of safety is prioritised. For instance, they are allowed and required to stop work being performed if they see a safety violation [1].
If you take safety seriously you're likely to have a safe workplace. If not you get these sorts of shenanigans.
> How do you train someone not to do stupid sh*t like that?
There needs good safety culture. If all your peers will give you an earful if they catch you doing something so visibly dangerous, you'll be less inclined to be a cowboy. Culture usually comes from the top down.