nukl
nukl

Reputation: 10531

arranging definitions

in python there is way to declare def's without putting it in class. So, if i just need to store many def's in separated class, i should simple do this:

def first_method():
    pass

def second_method():
    pass

and not use @classmethod or @staticmethod?:

class methods():

    @classmethod
    def first_method(cls):
        pass

    @staticmethod
    def second_method():
        pass

if first way is right, how should i mark private and public method?

Upvotes: 0

Views: 83

Answers (2)

SilentGhost
SilentGhost

Reputation: 319881

There is no such thing as private and public methods in Python. By convention, names that begin with a single underscore are considered "private", in a sense that user should not rely on their existence. Furthermore, you could document public interface. All the rest should of no relevance.

The set of mildly-related function would be a more pythonic solution, I'd say.

Upvotes: 3

Rafe Kettler
Rafe Kettler

Reputation: 76965

Yes. Just keep functions that are not instance methods out of your classes.

You're thinking like you would in Java. Python isn't Java. Python has no true way to have public and private methods -- you can change the names to indicate that you'd like some form of privacy (_ or __ at the beginning of the name), but it's not enforceable.

I've found that in all the time I've programmed in Python, I never wished for access modifiers.

Upvotes: 3

Related Questions