I am writing a function in some class in python, and people suggested to me to add to this function a @classmethod
decorator.
My code:
import random
class Randomize:
RANDOM_CHOICE = 'abcdefg'
def __init__(self, chars_num):
self.chars_num = chars_num
def _randomize(self, random_chars=3):
return ''.join(random.choice(self.RANDOM_CHOICE)
for _ in range(random_chars))
The suggested change:
@classmethod
def _randomize(cls, random_chars=3):
return ''.join(random.choice(cls.RANDOM_CHOICE)
for _ in range(random_chars))
I'm almost always using only the _randomize
function.
My question is: What is the benefits from adding to a function the classmethod
decorator?
self
. Also it means you can test it on the class without creating an instance. – Haidukfunctools.partial()
. Maybe. – Piperine