class-method Questions

5

Solved

I'm trying to learn the super() function in Python. I thought I had a grasp of it until I came over this example (2.6) and found myself stuck. http://www.cafepy.com/article/python_attributes_and_...
Elaterite asked 29/11, 2009 at 23:45

1

Solved

class Test(object): def __init__(self): pass def testmethod(self): # instance method self.task(10) # type-1 access class method cls = self.__class__ cls.task(20) # type-2 access class me...
Bootie asked 18/7, 2017 at 17:35

3

Solved

In below example Test class has two instance method and one classmethod In set_cls_var_1 method I set class variable using self. In set_cls_var_2 method I call class method using self. class Te...
Axseed asked 15/7, 2017 at 18:47

2

Solved

I want to implement a singleton pattern in python, and I liked the pattern described in the http://www.python-course.eu/python3_metaclasses.php. class Singleton(type): _instances = {} def __call...
Escharotic asked 22/6, 2017 at 16:5

1

Solved

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 = 'abc...
Ranged asked 16/4, 2017 at 12:15

3

Let's say we have a Pet class in Python: class Pet(object): num_pets = 0 def __init__(self, name): self.name = name Pet.num_pets += 1 def speak(self): print("My name's %s and the number of...
Latoria asked 13/4, 2017 at 21:13

4

Solved

I'm using Python 3. I know about the @classmethod decorator. Also, I know that classmethods can be called from instances. class HappyClass(object): @classmethod def say_hello(): print('hello') ...
Illomened asked 2/7, 2014 at 18:50

4

Solved

factory_boy defaults to 1 for sequences. How can I pass in a number to use as a different starting number instead? I can subclass the _setup_next_sequence() method, but how can I give it a variable...
Thill asked 14/3, 2013 at 5:59

2

Solved

I have been looking a the source code for peewee, specifically Model and the update function: https://github.com/coleifer/peewee/blob/a33e8ccbd5b1e49f0a781d38d40eb5e8f344eee5/peewee.py#L4718 I don...
Narda asked 19/2, 2017 at 2:18

18

Solved

What's the difference between a class method and an instance method? Are instance methods the accessors (getters and setters) while class methods are pretty much everything else?
Nice asked 27/6, 2009 at 20:48

1

I am learning the concept of classmethods in python. class A(): n=0 # object method def func_o(self): self.n += 1 print self.n # well, class method @classmethod def func_c(cls): cls.n +=...
Retard asked 17/1, 2017 at 11:54

0

Following compiles perfectly fine: struct MyClass { template<typename SameName> void foo (SameName* p); }; struct SameName {}; template<class SameName> void MyClass::foo (SameName* ...
Heilbronn asked 22/12, 2016 at 9:46

6

Solved

This is useful if you are trying to create class methods metaprogramatically: def self.create_methods(method_name) # To create instance methods: define_method method_name do ... end # To cre...
Calceolaria asked 15/4, 2009 at 17:3

2

Solved

Take the following example script: class A(object): @classmethod def one(cls): print("I am class") @staticmethod def two(): print("I am static") class B(object): one = A.one two = A.two...
Homoiousian asked 2/12, 2016 at 16:47

2

Solved

I have some python objects with some methods in which i would like to do some check at the beggining, depending of this check, the method's code would run, or an execption would be raised. Instead ...
Objectivity asked 10/11, 2016 at 13:17

0

I have a python module called my_module, within it I have a class called my_class and my_class has a classmethod called my_method. Based on other examples I have seen around I have come up with th...
Nocti asked 29/9, 2016 at 16:30

4

Solved

Are class methods and methods in the eigenclass (or metaclass) of that class just two ways to define one thing? Otherwise, what are the differences? class X # class method def self.a "a" end...
Consols asked 18/11, 2010 at 16:18

1

Solved

Is there any speed difference between class Methods and Static Methods? I am aware of the different use cases, but sometimes i could completely get rid of a class method and would like to know spee...
Pica asked 26/5, 2016 at 23:5

5

Solved

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,...
Pitterpatter asked 30/4, 2012 at 17:46

1

Solved

I am writing a generic class decorator which needs to apply a decorator to each method. My first approach is something like this: def class_decorator(cls): for name, member in vars(cls).items(): ...
Topography asked 9/2, 2016 at 12:49

1

Solved

I have a problem with the implementation of a decorator applied at this metaclass decorator I wrote: def decorateAll(decorator): class MetaClassDecorator(type): def __new__(meta, classname, sup...
Mechanistic asked 22/1, 2016 at 16:22

2

Solved

I want to override the method Selenium::WebDriver.for. This is what I tried: module SeleniumWebDriverExtension def self.for(browser, *args) if browser != :phantomjs super(browser, *args) else ...
Romelda asked 15/12, 2015 at 4:55

1

Solved

Many programming languages allow you to define class/instance methods, and the same for attributes. E.g. Python, Smalltalk. I have the concepts. E.g. for instance variables, every object has it’s o...
Sparge asked 4/10, 2015 at 18:10

2

Solved

I have a class which fetches details and populates the class with information if it's instantiated already with an id using a details method. If it's not instantiated yet I want it to instead use a...
Adamski asked 6/4, 2015 at 14:20

3

Consider two versions of a simple weather model which stores the location of clouds: class cloud: def __init__(self, x, y): self.x = x self.y = y collection = [] collection.append(cloud(1,2)) ...

© 2022 - 2024 — McMap. All rights reserved.