Hacker Newsnew | comments | show | ask | jobs | submit login

I don't like the fact that underscore and camelCase notations must be mixed, e.g. in the clock demo:

    def set_clock():
      ctx.beginPath()
      ctx.fillStyle = "#FFF"
      ctx.arc(width/2,height/2,ray*0.89,0,2*math.pi)
      ctx.fill()
      show_hours()
This is not the case with some other languages that compile to JavaScript, e.g. LiveScript allows dash-case almost everywhere.



They don't; it's purely a matter of convention that Python style guides prefer under_scored over camelCased. But you are entirely free to implement all of your python functions and variables and whatnot in camelCase, and in fact, many libraries do.

-----


You could probably even try to write a decorator that converted under_scores function names to camelCase for a nice little introduction into various parts of some of the more internal pieces of python.

-----


uhmm, AFAIK pep8 suggest UpperCamelCase for classes, under_score for packages, methods and functions[0]

btw: it's not a decorator :) but a camelCase->under_score renamer it's pretty easy to write

    import re
    
    def unCamel(obj, *maybe_camels):
        if not maybe_camels:
            maybe_camels = filter(lambda n: not n.startswith('_'), dir(obj))
    
        for maybe_camel in maybe_camels:
            underscore = re.sub(r'([^A-Z])([A-Z])([^A-Z])',
                lambda m: '%c_%c%c' % (m.group(1), m.group(2).lower(), m.group(3)),
                maybe_camel)
            if underscore != maybe_camel:
                setattr(obj, underscore, getattr(obj, maybe_camel))
    
    if __name__ == '__main__':
        class CamelCase(object):
            def multiWordMethod(self):
                print('called multiWordMethod')
        
        def multiWordFunction():
            print('called multiWordFunction')
    
        unCamel(CamelCase)    # magic
        import sys
        unCamel(sys.modules[__name__], 'multiWordFunction')    # magic
    
        CamelCase().multi_word_method()
        multi_word_function()



[0] http://www.python.org/dev/peps/pep-0008/

-----


Even in python standard library there are mixtures ie threading.Thread. Although those (as I understand it) are just remnants of old implementation that is kept for backwards compatibility.

-----


Isn't that normal? I though lowercase module name, uppercase class was the standard casing.

threading.Thread would seem to be consistent with the Google Python Style Guide (threading = module, Thread = Class)

http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...

-----


the methods of threading.Thread are camel case (ie getName)

http://docs.python.org/2/library/threading.html

-----


Yes, I think they ought to provide both styles;

Providing the camelCase will allow whole swathes of JS code to be cut-n-pasted; that code would be legal Python as well as JS.

-----




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: