class-method Questions
1
I've been trying to understand how I can specify the return type of a class method in Python, such that it is interpreted correctly (e.g. in my Sphinx documentation) even for child classes.
Suppose...
Frulla asked 1/5, 2020 at 0:52
5
Solved
So classmethods can be used as an alternative "constructor" in python, they are bound to the class and not to an instance, pretty clear so far. But my question is, if it is mandatory to h...
Seko asked 19/3, 2018 at 19:19
2
Solved
I'm trying to use the result of a class method several times without doing the heavy calculations required to obtain the result.
I am seeing the following options. Which ones do you think is the r...
Computerize asked 13/7, 2017 at 20:12
1
I'm want to use Python3 partialmethod and I can't call the resultant function. My code is based on the example from the Python documentation https://docs.python.org/release/3.6.4/library/functools....
Palladio asked 5/4, 2018 at 1:9
4
Solved
In Python, I can create a class method using the @classmethod decorator:
>>> class C:
... @classmethod
... def f(cls):
... print(f'f called with cls={cls}')
...
>>> C.f()
f calle...
Rozele asked 15/12, 2019 at 6:46
2
Solved
I come from java background, so I am slightly confused here.
Consider the code snippet below:
class A():
def __init__(self, **kwargs):
self.obj_var = "I am obj var"
@classmethod
def ...
Lorentz asked 15/1, 2016 at 5:54
1
Solved
I was trying to override __str__ and __repr__ for class as shown in the code below. The new methods are called whenever I call the instance_method but the object calls for class_method remains the ...
Incunabula asked 12/9, 2019 at 17:12
9
Solved
In python I can add a method to a class with the @classmethod decorator. Is there a similar decorator to add a property to a class? I can better show what I'm talking about.
class Example(ob...
Silo asked 4/3, 2011 at 4:34
2
What is the difference between class method vs. class field function vs. class field arrow function?
What is the difference between class method, class property which is a function, and class property which is an arrow function? Does the this keyword behave differently in the different variants of...
Reams asked 9/5, 2019 at 8:51
3
Solved
class C
end
var = "I am a local var outside"
C.class_eval do
def self.a_class_method
puts var
end
end
I know, this is not correct, because the def created a new scope.
I also know that use ...
Mangum asked 15/8, 2010 at 12:47
6
Solved
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...
Donkey asked 30/1, 2015 at 14:25
4
I have the following class and class variables:
class MyClass:
class_var_1 = "a"
class_var_2 = run_class_method()
@classmethod
def run_class_method(cls):
return "ran class method"
However,...
Retiary asked 10/2, 2019 at 21:39
1
Solved
I have never used @classmethod and I do not think of any examples to use it, I know how it works but I do not know when it's time to use it for example
class Example:
def __init__(self,para...
Hiltner asked 19/1, 2019 at 4:31
6
Solved
I have this code:
class SomeClass:
@classmethod
def func1(cls,arg1):
#---Do Something---
@classmethod
def func2(cls,arg1):
#---Do Something---
# A 'function map' that has function na...
Keniakenilworth asked 15/6, 2012 at 21:54
3
I have the following code:
class ObjectOne(object):
@classmethod
def print_class_name(cls):
print cls.__class__.__name__
def print_class_name_again(self):
print self.__class__.__name__
if _...
Missus asked 30/12, 2012 at 21:44
6
Solved
I'm using a Builder pattern in Python to separate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named ID... (e.g. ID12345). These all inherit from ...
Neutron asked 16/2, 2009 at 16:0
12
Solved
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, @classmeth...
Thermoplastic asked 29/8, 2012 at 13:37
1
Solved
A little background, I essentially need to define an int wrapper type, say MyInt (among some other classes), and another generic Interval type which can accept MyInt objects as well as other types ...
Krein asked 2/11, 2018 at 21:46
5
Solved
I have a class like:
class MyClass:
Foo = 1
Bar = 2
Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python?...
Flite asked 1/7, 2010 at 6:9
5
Solved
TL;DR How do I find out whether a function was defined using @classmethod or something with the same effect?
My problem
For implementing a class decorator I would like to check if a method take...
Cowley asked 7/10, 2013 at 14:40
5
Solved
Foo = Class.new
Foo.class_eval do
def class_bar
"class_bar"
end
end
Foo.instance_eval do
def instance_bar
"instance_bar"
end
end
Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Clas...
Diondione asked 22/5, 2009 at 23:32
1
Solved
I try to access the classmethod of a parent from within __init_subclass__ however that doesn't seem to work.
Suppose the following example code:
class Foo:
def __init_subclass__(cls):
prin...
Descant asked 18/4, 2018 at 0:7
1
Solved
I am able to test class methods just fine by using spies and Component.prototype. However, many of my class methods are class properties because I need to use this (for this.setState, etc.), and si...
Jordonjorey asked 31/3, 2018 at 0:3
1
Solved
I want to set some flags in my generic (before calling UseMethod(), I know that much :)), and then use and/or update these flags in the methods.
Like so:
g <- function(x) {
y <- 10
UseMet...
Spiers asked 14/2, 2018 at 8:19
1
Solved
How to get the class of an instance in TypeScript? This question is similar to Get an object's class name at runtime in TypeScript but I don't want to get the class name. I want to get the clas...
Pander asked 25/1, 2018 at 7:1
© 2022 - 2024 — McMap. All rights reserved.