Strategies for managing use of types in Python
        Posted  
        
            by 
                dave
            
        on Programmers
        
        See other posts from Programmers
        
            or by dave
        
        
        
        Published on 2011-02-08T23:03:33Z
        Indexed on 
            2011/02/09
            7:33 UTC
        
        
        Read the original article
        Hit count: 318
        
python
I'm a long time programmer in C# but have been coding in Python for the past year. One of the big hurdles for me was the lack of type definitions for variables and parameters. Whereas I totally get the idea of duck typing, I do find it frustrating that I can't tell the type of a variable just by looking at it. This is an issue when you look at someone else's code where they've used ambiguous names for method parameters (see edit below).
In a few cases, I've added asserts to ensure parameters comply with an expected type but this goes against the whole duck typing thing.
On some methods, I'll document the expected type of parameters (eg: list of user objects), but even this seems to go against the idea of just using an object and let the runtime deal with exceptions.
What strategies do you use to avoid typing problems in Python?
Edit: Example of the parameter naming issues: If our code base we have a task object (ORM object) and a task_obj object (higher level object that embeds a task). Needless to say, many methods accept a parameter named 'task'. The method might expect a task or a task_obj or some other construct such as a dictionary of task properties - it is not clear. It is them up to be to look at how that parameter is used in order to work out what the method expects.
© Programmers or respective owner