Python: How To Assign Shader To An Object?

import maya.cmds as cmds

cmds.sphere()

myBlinn = cmds.shadingNode('blinn', asShader=True)

cmds.select( 'nurbsSphere1' )

cmds.hyperShade( myBlinn, assign=True )

Whats wrong in this code?

CODE
cmds.hyperShade( assign=myBlinn)

I prefer using the set function, and control everything myself. A bit more complex but you get much more control. Plus since the hyperSahder function works only on a selection, you don't have to select objects before assigning them, this will be faster in large scene and provide you with better control in the long run.

CODE
import maya.cmds as mc

def getSGfromShader(shader=None):
    if shader:
        if mc.objExists(shader):
            sgq = mc.listConnections(shader, d=True, et=True, t='shadingEngine')
            if sgq:
                return sgq[0]

    return None

def assignObjectListToShader(objList=None, shader=None):
    """
    Assign the shader to the object list
    arguments:
        objList: list of objects or faces
    """
    # assign selection to the shader
    shaderSG = getSGfromShader(shader)
    if objList:
        if shaderSG:
            mc.sets(objList, e=True, forceElement=shaderSG)
        else:
            print 'The provided shader didn\'t returned a shaderSG'
    else:
        print 'Please select one or more objects'

def assignSelectionToShader(shader=None):
    sel = mc.ls(sl=True, l=True)
    if sel:
        assignObjectListToShader(sel, shader)

assignSelectionToShader('lambert2')

ho and also you can continue these function to get the sg from an object for example. This way it becomes easy to assign the shader of the first selected object on the second selected object...