How to select the last created object as the active object?

I am very new at scripting and I am pretty lost. I want my script being able to duplicate the selected faces then separate it to a new object using separate-selection, and then make the new object as the active. Thanks for your attention

Try this:

import bpy

actob = bpy.context.active_object

bpy.ops.mesh.duplicate()

bpy.ops.mesh.separate(type='SELECTED')

bpy.ops.object.editmode_toggle()

bpy.context.scene.objects.active = actob

bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

bpy.ops.object.parent_set(type='OBJECT', keep_transform=False)

for actob in actob.children:
    actob.select = True
    bpy.context.scene.objects.active = actob
bpy.ops.object.parent_clear(type='CLEAR')
bpy.ops.object.select_all(action='DESELECT')
actob.select = True

@Albertofx This is incredible, so smart and simple :smiley:

For anyone that doesn’t code, just slap a bpy.ops.object.mode_set(mode=‘EDIT’, toggle=False) at the end and you have a perfect
extract script.