Questions tagged [class-method]

Methods that are called on a class instead of on an object.

Class methods are methods that are called on a class (compare this to class instance methods, or object methods). Its meaning may vary depending on the programming language: In some languages (e.g. C++, Java), class methods are synonymous with static methods (see section below), which are called with a known class name at compile-time. this cannot be used in static methods.

In some other languages (e.g. Smalltalk, Ruby, Objective-C), class methods are methods that are called on a class object, which can be computed at runtime, there being no difference between calling a method on a regular object or a class object; thus both instance and class methods are resolved dynamically, and there are no "static" methods. Notably, in these class methods, this refers to the class object.

Some languages have both. For example, in Python, one can create class methods and static methods using the classmethod and staticmethod decorators, respectively. The former has access to this (i.e. the instance object, conventionally known as self), while the latter does not.

904 questions
0
votes
2 answers

Creating methods and testing a class in Java

I am currently working on the following questions as revision: A VolumeModel stores the data for a volume control object. The volume has level and it can also be put in a muted state. (The following underneath is what is drawn in a UML…
user1279780
  • 99
  • 3
  • 10
0
votes
1 answer

NSMutableArray in Class Method

I've created a class method that returns a random position as an NSValue object. I want to store the returned positions in a mutable array so the next time the class method gets called it checks the positions in the mutable array and if it finds one…
Ramin Afshar
  • 989
  • 2
  • 18
  • 34
-1
votes
1 answer

classmethod with different overloaded signature between instance and base class

I am trying to write a class with an additional constructing method that accepts extra values. These extra values are expensive to compute, and are saved at the end of the program, so .initialize() effectively serves as an injection to avoid…
Some Guy
  • 576
  • 1
  • 4
  • 17
-1
votes
1 answer

Is it good practice in python to store auth data in class attribute?

I want to access multiple google calendars from python: there is a primary calendar connected to a google account, also other (secondary) calendars can be created. Access to the secondary calendars is possible after google authorization to the…
zeliboba7
  • 335
  • 1
  • 10
-1
votes
2 answers

Why is the instance variable not getting changed?

class Player: def __init__(self, Name, Age, Sex): self.Name = Name self.Age = Age self.Sex = Sex self.validate(Name,Age,Sex) @classmethod def validate(cls, Name, Age, Sex): if…
Pritom Roy
  • 13
  • 2
-1
votes
1 answer

In the method "from_dash" when we return "cls(*string.split("-"))" , what exactly is being assigned to the object "karan"? Please someone explain me

class Employee: def __init__(self, aname, asalary, arole): self.name = aname self.salary = asalary self.role = arole def printdetails(self): return f"The Name is {self.name}. Salary is {self.salary} and…
arin
  • 29
  • 4
-1
votes
2 answers

Why is it not printing a string when i call a class method

Im just playing around with python OOP, and when I call this class method I get <__main__.User object at 0x7fb889516220> code class User: users = [] def __init__(self, name): self.name = name self.users.append(self) @classmethod …
yoda
  • 33
  • 1
  • 4
-1
votes
1 answer

python @classmethod returning 'nonetype'

new to python here and still working through some growing pains. Due to poor source control management, I lost the original code that had this class method working. My intent here is to get the class method to take inputs from the user and assign…
Krys M
  • 1
-1
votes
1 answer

Why not classmethod in this line?

I read this method in a repository by huggingface: @staticmethod def from_file(vocab: str, **kwargs): vocab = WordPiece.read_file(vocab) return BertWordPieceTokenizer(vocab, **kwargs) It returns an instantiation of a class, then I wonder…
Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
-1
votes
1 answer

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 hasMoreFactors. The nextFactor method must return an integer…
munchy
  • 1
  • 1
-1
votes
1 answer

Python @classmethod - is 'cls' just a coding style requirement?

Where is the difference between this: class Dummyclass: value = 4 def dummymethod(value): Dummyclass.value = value Dummyclass.dummymethod(31) and: class Dummyclass: value = 4 @classmethod def dummymethod(cls, value): …
-1
votes
1 answer

Incrementing age and determining if adult with classes and methods

I am trying to write a class that determines if a person is an adult or not. If a person is an adult then they are 18 or older. And the program should return True. However, I keep getting a return value of False when I implement this program. If…
DataD96
  • 301
  • 3
  • 12
-1
votes
1 answer

Is it against pep style rules to call a class method outside of its class?

The code works as intended I was just wondering if it is poor practice to call a class method outside of the class that it was defined in. Example shown below class A: @staticmethod def _print_some(): print("something") …
Michael O.
  • 11
  • 5
-1
votes
1 answer

Calling @classmethod's super, which calls another classmethod

Could not find an answer here, so posting this. Was getting this error: main() takes 1 positional argument but 2 were given For this code, where B tries to extend A's method, which intern calls an additional class method. class A: @classmethod …
run_the_race
  • 1,344
  • 2
  • 36
  • 62
-1
votes
1 answer

How to use class methods in python?

I have this code class Person(object): def __init__(self, name): self.name = name @classmethod def from_classmethod(cls, name): return cls(name) p = Person.from_classmethod("Moctar") p.name But it shows the following…
moctarjallo
  • 1,479
  • 1
  • 16
  • 33