Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to call an instance method from a class method of the same class

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': random.randint(0, 10)}

    @classmethod
    def new(cls):
        params = cls.get_params()
        return cls(**params)

and I would like to be able to do:

>>> obj = MyClass.new()
>>> obj.int  # must be defined
9

I mean without creating a new instance of MyClass, but obviously it's not that simple, because calling MyClass.new() throws TypeError: unbound method get_params() must be called with MyClass instance as first argument (got nothing instead)

Is there any way to accomplish so? Thanks

like image 639
Gerard Avatar asked May 07 '13 19:05

Gerard


People also ask

How do you call an instance method from the class method?

Bound method (instance call): To call the method we must provide an instance object explicitly as the first argument. In other words, a bound method object is a kind of object that remembers the self instance and the referenced function. So, a bound method may be called as a simple function without an instance later.

Can methods call other methods in the same class?

Similarly another method which is Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.

How do you call a class method from another class method in Python?

Call method from another class in a different class in Python. we can call the method of another class by using their class name and function with dot operator. then we can call method_A from class B by following way: class A: method_A(self): {} class B: method_B(self): A.

Can an instance method call another instance method?

Instance methods can access instance variables and instance methods directly. This means a method that doesn't have a static modifier i.e. an instance method can access any non-static variable as well as call any non-static method.


2 Answers

No, you can't and shouldn't call an instance method from a class without an instance. This would be very bad. You can, however call, a class method from and instance method. Options are

  1. make get_param a class method and fix references to it
  2. have __init__ call get_param, since it is a instance method

Also you may be interested in an AttrDict since that looks like what you are trying to do.

like image 109
cmd Avatar answered Sep 19 '22 13:09

cmd


You can call instance method with classmethod when treating it like instance method and add class as instance. IMO it is really bad practice, but it solves your problem.

@classmethod
def new(cls):
    params = cls.get_params(cls)
    return cls(**params)
like image 36
Sajmon Avatar answered Sep 18 '22 13:09

Sajmon