class-method

Why should I use a classmethod in python?

Why should I use a classmethod in python? Question: 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 …

Total answers: 1

Declaring new variables inside class methods

Declaring new variables inside class methods Question: I just read about class and method variables in Python, and I am wondering if there is a difference between these two examples: class Example(object): def __init__(self, nr1, nr2): self.a = nr1 self.b = nr2 def Add(self): c = self.a + self.b return c class Example2(object): def __init__(self, …

Total answers: 5

Same name for classmethod and instancemethod

Same name for classmethod and instancemethod Question: I’d like to do something like this: class X: @classmethod def id(cls): return cls.__name__ def id(self): return self.__class__.__name__ And now call id() for either the class or an instance of it: >>> X.id() ‘X’ >>> X().id() ‘X’ Obviously, this exact code doesn’t work, but is there a similar …

Total answers: 6

Python: How to call an instance method from a class method of the same class

Python: How to call an instance method from a class method of the same class Question: I have a class as follows: class MyClass(object): int = None def __init__(self, *args, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) def get_params(self): return {‘int’: random.randint(0, 10)} @classmethod def new(cls): params = cls.get_params() return cls(**params) and I …

Total answers: 3

Why use classmethod instead of staticmethod?

Why use classmethod instead of staticmethod? Question: I know what they do and I’ve seen many examples of both, but I haven’t found a single example where I would have to use classmethod instead of replacing it with a staticmethod. The most common example of classmethod I’ve seen is for creating a new instance of …

Total answers: 3

Meaning of @classmethod and @staticmethod for beginner?

Meaning of @classmethod and @staticmethod for beginner Question: What do @classmethod and @staticmethod mean in Python, and how are they different? When should I use them, why should I use them, and how should I use them? As far as I understand, @classmethod tells a class that it’s a method which should be inherited into …

Total answers: 12

When should I use @classmethod and when def method(self)?

When should I use @classmethod and when def method(self)? Question: While integrating a Django app I have not used before, I found two different ways to define functions inside the class. The author seems to use them both distinctively and intentionally. The first one is the one that I myself use a lot: class Dummy(object): …

Total answers: 4

Static classes in Python

Static classes in Python Question: I once read (I think on a page from Microsoft) that it’s a good way to use static classes, when you don’t NEED two or more instances of a class. I’m writing a program in Python. Is it a bad style, if I use @classmethod for every method of a …

Total answers: 5

super and __new__ confusion

super and __new__ confusion Question: As what I just learned, I can use super() this way: super(class, obj_of_class-or-_subclass_of_class) Code goes below: #Case 1 class A(object): def __init__(self): print “A init” class B(A): def __init__(self): print “B init” super(B, self).__init__() #ok, I can invoke A’s __init__ successfully #Case 2 class A(object): @classmethod def foo(cls): print “A …

Total answers: 1

What's an example use case for a Python classmethod?

What's an example use case for a Python classmethod? Question: I’ve read What are Class methods in Python for? but the examples in that post are complex. I am looking for a clear, simple, bare-bones example of a particular use case for classmethods in Python. Can you name a small, specific example use case where …

Total answers: 6