Is there a way to explicitly set the mode in python? I'm developing a basic sculpting addon for practice and one of the features I'd like to include in this addon requires modes to be set explicitly.

Here's an example of the type of api that I'm looking for.

if bpy.context.mode != 'OBJECT':
    bpy.ops.set_mode('OBJECT')


I tried looking for something on my own, but I only found methods that toggle the mode.

Is there a method like what I was describing that would do the job?
You can go directly to a specific mode using -

bpy.ops.object.mode_set(mode='EDIT', toggle=False)


Valid mode values are 'OBJECT', 'EDIT', 'SCULPT', 'VERTEX_PAINT', 'WEIGHT_PAINT', 'TEXTURE_PAINT', 'PARTICLE_EDIT', 'POSE' but not all will be accepted at anytime, eg PARTICLE_EDIT will only be accepted if the object has a particle system.