E
E
exctac2018-02-22 14:22:17
Django
exctac, 2018-02-22 14:22:17

PEP 8: Class method before properties?

Good afternoon!
Usually it is customary to describe class methods after properties, the question will not be critical if you describe it before them, and not after? Namely in the django model:

def get_default_sound(self):
    pass

class A(model.Model):
    sound = models.CharField(_('Название'), max_length=255, default=get_default_sound)

Is it possible to do so?
class A(model.Model):
    def get_default_sound(self):
        pass

    sound = models.CharField(_('Название'), max_length=255, default=get_default_sound)

The matter is that to take out function for a class when this function is necessary only for a class, not absolutely beautifully and not explicitly looks in the code. but in the class before the property to describe it is also somehow not very good. If described under the property, then the default parameter does not see the function.
How to be?))) I would like to know exactly the opinion based on the accepted offs. agreements, without holivar.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pugachev, 2018-02-22
@exctac

exctac ,
1) you need to cut out pep-8 from tags
2) the question here is not in conventions but in the logic of your application
if the get_default_sound function is essentially static or should be rummaged between classes - then why not have it in one instance at the module level
if it is does not mess around with anyone - it is advisable not to take it out of the scope of the class,
as the classics said, "keep the definition as close to use as possible." this concerns not only the location of lines of code, but also the location in the stack of scopes,
so as for me, the issue here is not in agreements, but in the architecture of your application,
if you want to be consistent with django, then take a look here for example
https://docs.djangoproject. com/en/2.0/ref/contrib/...
Here are both options

class A(object):

    @staticmethod
    def get_val():
        return 42

    val = get_val.__func__()


print A.val

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question