Does a syntax for this exist? In any language?
        Posted  
        
            by Michael
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Michael
        
        
        
        Published on 2010-05-28T03:58:09Z
        Indexed on 
            2010/05/28
            4:01 UTC
        
        
        Read the original article
        Hit count: 231
        
It seems pretty common to me to have an argument, in a dynamically typed language that is either an Object or a key to lookup that object. For instance when I'm working with a database I might have a method getMyRelatedStuff(person)
All I really need to lookup the related stuff is the id of the person so my method could look like this in python:
def getMyRelatedStuff(person_or_id):
    id = person_or_id.id if isinstance(person,User) else person_or_id
    #do some lookup
Or going the other direction:
def someFileStuff(file_or_name):
    file = file_or_name if hasattr(file,'write') else open(file_or_name)
        © Stack Overflow or respective owner