How to hide unused Node socket using python script
You can either call hide.socket_toggle() operator on all nodes in selection or hide each socket one by one using NodeSocket.hide property. Following demo is based on Operator Node template:

import bpy

def main(operator, context):
    node_selected = context.selected_nodes

    if node_selected:
        bpy.ops.node.hide_socket_toggle()
    else:
        operator.report({'ERROR'}, "Nothing selected")
        return

class NODE_OT_Operator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "node.simple_operator"
    bl_label = "Hide Node Sockets Operator"

    @classmethod
    def poll(cls, context):
        space = context.space_data
        return space.type == 'NODE_EDITOR'

    def execute(self, context):
        main(self, context)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(NODE_OT_Operator)

def unregister():
    bpy.utils.unregister_class(NODE_OT_Operator)

if __name__ == "__main__":
    register()



Result of calling the operator on all selected nodes in the shader editor


Result of calling the operator on all selected nodes in the compositor



Demo on how to hide the alpha sockets of Render Layer and Composite node using the console:

&gt;&gt;&gt; rl_node = C.scene.node_tree.nodes.get('Render Layers')
&gt;&gt;&gt; alpha_socket = rl_node.outputs.get("Alpha")
&gt;&gt;&gt; alpha_socket.hide = True
&gt;&gt;&gt;
&gt;&gt;&gt; comp_node = C.scene.node_tree.nodes.get('Composite')
&gt;&gt;&gt; alpha_socket = comp_node.inputs.get("Alpha")
&gt;&gt;&gt; alpha_socket.hide = True



Result of setting the state for each output or input socket