Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for descriptors

I have a descriptor

class ReferredItem(): 
    def __init__(self, method):
        self.method = method

    def __get__(self, obj, objtype):
        ...

I use it as decorator:

class MyClass():

    @ReferredItem
    some_method(self):
        ...

I've seen the decorators are lower case. But classes should be named in camel case.

Should i name the class like referred_item? Or leave it as it is now?

like image 759
warvariuc Avatar asked Apr 18 '12 15:04

warvariuc


Video Answer


2 Answers

PEP8 states that

Almost without exception, class names use the CapWords convention.

without explaining what the exceptions are, but in the standard library, classes that are most commonly used as functions usually follow the function naming convention. E.g. itertools.groupby is actually a class, but you don't notice that in ordinary usage; it's an implementation detail and groupby could be rewritten as an actual function.

You can adopt a similar style by using the all-lowercase decorator naming convention for classes used as decorators: referred_item, not ReferredItem.

like image 159
Fred Foo Avatar answered Oct 13 '22 21:10

Fred Foo


I usually use CamelCase for the class and then add an alias which I use as decorator.

referred_item = ReferredItem
like image 25
yak Avatar answered Oct 13 '22 21:10

yak