class-method Questions

3

Solved

I am unsuccessfully trying to get the magic with-statement methods __enter__ and __exit__ running on class-level: class Spam(): @classmethod def __enter__(cls): return cls @classmethod def ...
Holly asked 4/2, 2015 at 20:5

4

Solved

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': ran...
Oistrakh asked 7/5, 2013 at 19:52

8

Solved

Other than self.class.send :method, args..., of course. I'd like to make a rather complex method available at both the class and instance level without duplicating the code. UPDATE: @Jonathan ...
Antung asked 21/8, 2008 at 18:2

7

Solved

Ex. If I have something like this: class C(object): @classmethod def f(cls, x): return x + x This will work: c = C() c.f(2) 4 But is that bad form? Should I only call C.f() or c.__c...
Punishable asked 28/3, 2009 at 2:21

5

Solved

I've worked with a few different languages such as Java, C#, and Objective-C. In most languages, methods that don't require an instance of an object are called static methods. However, when it com...

4

I would like to include a class method as my options when using the inclusion validation: class Search < ActiveRecord::Base attribute :foo, Array validates :foo, inclusion: class_method_opti...
Tenacious asked 21/8, 2014 at 1:54

2

Solved

I'd like to automatically run some code upon class creation that can call other class methods. I have not found a way of doing so from within the class declaration itself and end up creating a @cla...
Leralerch asked 24/8, 2012 at 19:13

36

Solved

What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?
Expensive asked 25/9, 2008 at 21:1

1

Solved

See the below Python 3.10 snippet: import contextlib class Foo: @contextlib.contextmanager @classmethod def state(cls): try: yield finally: pass with Foo.state(): pass It throws a TypeEr...
Adulterine asked 4/10, 2022 at 17:47

5

Solved

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 ...
Galvani asked 2/3, 2017 at 14:48

11

Solved

Is there a way to bind a decorator to all functions within a class generically, rather than explicitly stating it for every function? I suppose it then becomes a kind of aspect, rather than a decor...
Haldan asked 12/8, 2010 at 12:16

7

Solved

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. ...
Tailgate asked 21/4, 2011 at 1:19

3

Solved

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 ...
Epigrammatist asked 14/5, 2012 at 15:54

2

I want to make my class method runs parallel, but it only produces some kind of error that I can not solve. My code is: import concurrent.futures as futures samples = ['asfd', 'zxcv', 'asf', 'qwer...

4

Solved

I want to override a method and pass different argument types to it: class Base { public myMethod(myString: string): undefined { return; } } class Child extends Base { public myMethod(myNumbe...
Julienne asked 6/6, 2018 at 20:57

4

Solved

I have a Python class that contains all the AWS regions. I wrote a class method that would return me the list of all the regions. Is there a better way of returning all the class variable values so...
Rangoon asked 27/6, 2017 at 21:15

2

Solved

Consider the following metaclass/class definitions: class Meta(type): """A python metaclass.""" def greet_user(cls): """Print a friendly greeting ide...
Slattery asked 22/8, 2021 at 16:7

3

class MetaData(): maxSize = 2**10 # class definition code if not os.path.exists('sample.data'): SSD = open('sample.data', 'wb+') data = { 0: [], 1: {'.': None,} } data[1]['~'] = data[1] ...
Chimb asked 9/1, 2021 at 17:16

1

Solved

I just started working with CodeIgniter 3, I understand that it is already outdated, but the task is set to work with it. And I immediately had a problem, rummaged through the Internet and did not ...
Galloot asked 22/12, 2020 at 8:8

18

Solved

I'm teaching myself Python and my most recent lesson was that Python is not Java, and so I've just spent a while turning all my Class methods into functions. I now realise that I don't need to use...
Luisluisa asked 1/9, 2008 at 18:16

4

Solved

Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class Derived(Base): @classmethod def do(cls, a): print 'In derived!' # Base.do(cls, a) -- can'...
Bortz asked 12/8, 2009 at 23:7

4

Solved

I thought that the following code would result in an error because as far as I have read, a method in a Python class must either have "self" (or any other label, but "self" by convention) as its fi...
Wisconsin asked 16/10, 2018 at 8:51

1

Solved

Update 2024 Full blogpost: https://dev.to/dannyengelman/web-component-developers-do-not-connect-with-the-connectedcallback-yet-4jo7 Update March 2023 Note: this works because in the next tick your ...
Bound asked 23/5, 2020 at 12:20

2

Solved

module Country def location puts "location" end def self.included(base) def cities puts "cities" end end def self.extended(base) def animals puts "animals" end end end class Test i...

2

Solved

New to Python and having done some reading, I'm making some methods in my custom class class methods rather than instance methods. So I tested my code but I hadn't changed some of the method calls...
Negress asked 12/5, 2015 at 11:52

© 2022 - 2024 — McMap. All rights reserved.