Questions tagged [class-method]
Methods that are called on a class instead of on an object.
765
questions
0
votes
0answers
12 views
Returning class with updated instance values using dataclass
I am trying to learn a bit more about using dataclasses and have created some classes that will be fed into a Multi-Arm Bandit Model:
@dataclass
class DefaultBanditParameters:
"""
...
0
votes
2answers
53 views
Assigning an attribute value in a PYTHON class with class methods
This is a very basic question relating to setting attributes that I don't find a definitive answer to.
I have a python class as follows
class circle():
PI = 3.14
def __init__(self,...
0
votes
1answer
45 views
Calling an instance method from a class method - Ruby
So I have this class:
class MyClass
@@variable = 9
def initialize
@@variable -= 1
end
def self.tracker
puts @@variable
if @@variable <9
puts "#{...
1
vote
1answer
22 views
ELI5: Why can you call class methods within class methods without explicitly stating the class itself? (Ruby/Rails) [duplicate]
Sorry, the title is kind of confusing but I just discovered something that's quite mindboggling. When you call class methods inside class methods, you don't have to put a self before it in order for ...
2
votes
0answers
47 views
How are decorators implemented with a class in Python?
def decorator_function(original_function):
def wrapper_function():
print('wrapper_function executed before {}'.format(original_function.__name__))
return original_function()
...
0
votes
0answers
11 views
Python: Overload Instance Method and Class Method/ Access Instance Variable from Class Method
I was wondering what the best way to implement the following design would be in Python:
class Executor:
def __init__(self):
self.val = 5
def action(self):
self.action(self.val)...
2
votes
1answer
74 views
The best way to deprecate all class methods in Rails
I have a class (and it is not an ActiveRecord model) having multiple class methods. All the class methods must be deprecated. What's the best way to do this?
class MyClass
class << self
...
0
votes
0answers
45 views
Javascript .multiply 'function cannot be found'
I am playing around with trekhleb's javascript-algorithms repo - specifically the Fast Fourier Transformation. The code can be found here:
https://github.com/trekhleb/javascript-algorithms/blob/master/...
0
votes
1answer
22 views
I'm having trouble calling methods belonging to certain classes within methods of an alternate class
This is my first time asking a question so if I could be doing something better please let me know.
I have three classes, all which need to be working together. Here's an example:
class CLI
def self....
0
votes
1answer
26 views
Using descriptors or properties with classmethod
I've seen many answers to that question, all of them saying it's impossible to use a property with a classmethod, but the following code works:
class A:
@classmethod
@property
def hello(...
0
votes
0answers
25 views
What s the difference between Class and Static methods in Python, since in a static method you can just access the class by name instead of cls? [duplicate]
Here is an example of where class and static methods have the same behavior.
class MyClass:
data = 'stuff'
@classmethod
classModifyData(cls, newData):
cls.data = newData
@...
1
vote
0answers
35 views
Class method for adding custom provider to Faker
I am using the Python Faker API and I want add a custom provider to it. I want register my custom provider with the class Faker and not the instance. As I read here, it seems we can register the ...
0
votes
1answer
56 views
Python classmethod constructor inheritance with different signature
TLDR;
I am using a @classmethod as a constructor for my class, and I need to override it with a different signature for one specific child class that needs extra parameters. PyCharm gives a warning ...
1
vote
0answers
44 views
TypeScript explicit `this` parameter in class/interface method
For now I use TypeScript and encountered feature(?) I can't figure out on my own by using Google. Could anyone make this clear? The strange thing regards to explicit this declaration of a class method....
1
vote
1answer
38 views
Wrapping a C++ class method in a try catch block with added functionality
Coming from mostly interpreted and dynamically typed languages, I'm a little stuck on how to approach this problem in C++.
I have a class Child which inherits from Parent and adds some functionality. ...
1
vote
3answers
126 views
How do I call a class function inside the class definition?
class MetaData():
maxSize = 2**10
# class definition code
if not os.path.exists('sample.data'):
SSD = open('sample.data', 'wb+')
data = {
0: [],
...
0
votes
1answer
38 views
Class method: Where is the difference between function(self) and function?
I am a bit confused.
The following two python code snippets both return the same values:
First:
class Test():
def __init__(self):
None
def outer_function(self):
self.i = &...
0
votes
0answers
23 views
How to call cache clear of a class method?
I have this code:
class Test(CqlEngineModel):
@classmethod
@cql_engine_connector
@functools.lru_cache(maxsize=1000)
def is_not_analyzable(cls, x, y):
if mic:
...
0
votes
0answers
37 views
Parallel execution of class methods raises PicklingError
I implemented a class, which calculates possible configurations for different battery types..
Therefore, all battery data is stored in a nested dictionary, which is a property of the class itself. ...
1
vote
0answers
64 views
Address of Class method crash on x64
The AddressOf operator works only with methods inside standard .bas modules. I am using the following code to retrieve the addresses of class methods:
Option Explicit
#If VBA7 Then
Private ...
1
vote
1answer
40 views
How does one use the Try-Catch-Finally block in a class method?
I get contradictory error messages when trying to set up a try-catch-finally block in a class method which returns a string.
My code is:
class exampleClass {
[string]Create() {
try {
...
-1
votes
1answer
61 views
Did I correctly create a Boolean class? How do I fix the 'illegal start of expression' and ';' expected errors in my code?
I am trying to write a program that will take the integer input from a user and print out its factors.
I'm trying to do this by creating a class FactorGenerator and methods nextFactor and ...
1
vote
1answer
44 views
classmethod inheritance and overwrite class parameter
I struggle to understand how to assign a value to PersonProfile from a class method when PersonProfile inherits from PersonBase.
class PersonBase:
def __init__(self, contact_no=None, email=None, ...
3
votes
2answers
38 views
How can I use the string in the list as string methods in python?
I am trying to print some given string in three different ways provided by string methods which are inside a list as strings. Is there a way to convert those strings as methods to use it during loop ...
0
votes
1answer
21 views
Can I use the arrow function within a class method?
In this post, I saw closure was implemented, but I want to keep this class method while still being able to access the class properties. My goal is to pass this innerFunc to the setTimeOut(), but ...
0
votes
1answer
79 views
Classmethods on generic classes
I try to call a classmethod on a generic class:
from typing import List, Union, TypeVar, Generic
from enum import IntEnum
class Gender(IntEnum):
MALE = 1
FEMALE = 2
DIVERS = 3
T = ...
0
votes
1answer
46 views
Can an object call on a method in the class it was instanced?
Basically I have an object of a class that I instance in another class. Inside the other classmI have a method which I would like call from inside the object class. Is there any way to do this?
public ...
0
votes
1answer
16 views
Python class method: Extracting two of eight attributes from a class and assigning to a list
I have created a class(City) with several parameters-- name, latitude, longitude, country_name, city_population, population_ratio. population_ratio is created by dividing the City's population by the ...
0
votes
3answers
55 views
Defining a classmethod outside of a class in Python [closed]
I am making a game with Pygame where there are going to be many different screens. I am making a class called Screen that will have custom screens made for you. I need each screen to have different ...
0
votes
1answer
43 views
R plot method for custom S4 class
I'm developing an R package and want to create a plotting function for my custom S4 class. It should work like this:
setClass("Person", representation(name = "character"))
me <- ...
0
votes
1answer
16 views
Why changing object itself through class object doesn't work?
class Cat:
def func(self):
self = None
a = Cat()
print(a)
a.func()
print(a)
I thought I would get None with second print function, but I got same object addresses for both prints. Why ...
1
vote
1answer
52 views
Does an Array of a large class (with lots of unused methods) take more time to compute than with smaller classes?
I was wondering the following thing:
I have an array of a "Projectile" class for a 2d game. Every Projectile works a little bit different and thus needs different methods. Im beginning to ...
0
votes
1answer
36 views
Rails API Strange behaviour on class method load
The project setup :
I'm running a Rails 6.0.3.4 / Ruby 7.1.2 API linked with Mongoid to MongoDb 4.4.1, and got a controller such as :
module Api
module V1
class ApplicationController < ...
0
votes
1answer
28 views
How do I establish the average grade for all students?
I have tried to make a method where I can show the entire average of all students. As you can see in my code, I tried to create an empty list (class-variable) to store the average grade of each ...
2
votes
1answer
45 views
Cython: Workaround scipy.optimize.cython_optimize.brentq argument ctype to work with class methods ctype
I'm trying to use the root finder scipy.optimize.cython_optimize.brentq inside a class, but the first argument of this function accepts only the type ctypedef double (*callback_type)(double, void*) ...
0
votes
0answers
29 views
Is there any practical difference between accessing attributes and methods throught @staticmethod or @classmethod?
I understand that static methods are meant to perform operations that do not access methods or attributes within the same class. They simply perform specific operations that do not need the class ...
0
votes
0answers
74 views
How do I prevent methods from being overwritten and assigned a different type?
I want to protect all class methods and prevent them from being overwritten, e.g. Object.assign(dog, { bark: true});. Someone said that for this reason you should not use class methods in Typescript ...
0
votes
2answers
43 views
Group the List of Dictionary into Class objects
I have a list of dictionary as follows with 4 keys
dict_list = [
{'key1': 'a', 'key2':'b' , 'key3':'p' , 'key4':'q' },
{'key1': 'a', 'key2':'b' , 'key3':'p' , 'key4':'r' },
{'key1': 'a', 'key2':'c' , '...
2
votes
1answer
46 views
Difficulty calling a class method in rails. Have other class methods that work fine
So I am trying to write a class method to send a csv file using a mailer class. I think I have that logic all worked out and the issue I am experience is a NoMethodError. I have defined the method ...
1
vote
1answer
91 views
Trying to access R class methods and fields within Python using rpy2
I am using rpy2 to import a library from CRAN repository called "MatrixEQTL" to run in within Python using importr, here is my attempt:
import rpy2.robjects as robjects
from rpy2.robjects....
0
votes
1answer
68 views
How to overcome “classmethod is not callable” in python
After reading Various errors in code that tries to call classmethods,
Why are python static/class method not callable?,
Are staticmethod and classmethod in python not callable? and others it is clear ...
0
votes
2answers
84 views
static method can't access caller class name
I have 2 classes A and B where B extends A. Both class are having a static variable x. There is one static method defined in class A which is accessing x. When I call the method with B from main, is ...
1
vote
4answers
54 views
In Python, why does a class method override an instance method?
See code below:
class MyClass:
# instance method.
def printline(self):
print('This is an instance method!')
@classmethod
def printline(cls):
print('This is a class ...
0
votes
1answer
60 views
Python class creates attributes in order
I am hoping to do some calculation using class methods and create a new attribute for each function (create_d and create_e). It doesn't feel very elegant because one has to run create_d before ...
0
votes
0answers
30 views
Is there a way to subclass using an alternate constructor class method?
For example, say I have a parent class:
class Foo:
def __init__(self, x, y):
self.x = x
self.y = y
@classmethod
def special_pointx(cls, y):
return cls(1, y)
And then I wanted to create a ...
1
vote
1answer
36 views
Get class property from jquery callback
I have a class:
class MyClass{
#arr= [];
constructor(){
$("body").on("click", ".myClass", function(e){
//here, i need to access this.#arr, but ...
1
vote
1answer
32 views
How to properly inherit class method
I have a database connection class that creates a connection pool. Now as the application grows and I'm adding different types of database writers, I want to move database connections to a separate ...
0
votes
0answers
25 views
Changing state of a class in Python using a function with @classmethod decorator
I was reading about the differences in @classmethod and @staticmethod for Python. I know that the class as a parameter is passed to the @classmethod functions, and not to the @staticmethod functions. ...
0
votes
0answers
40 views
Which are the differences between UseMethod and .S3method()?
I am new in package development. And I am joining in a package some of my functions I use a lot for personal use... Studying how package is developed, I consider S3 object types interesting... But I ...
0
votes
2answers
96 views
Python class polymorphism
I am practicing polymorphism and the following code returns error
class Animal:
def talk(self, something):
print(something)
class Dog(Animal):
def talk(self):
super().talk(&...