Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple objects within the same class in Python

Tags:

python

I want to be able to return multiple objects of a class by using a method within the class. Something like this.

class A:
    def __init__(self,a):
    self.a = a

    def _multiple(self,*l):
        obj = []
        for i in l:
            o = self.__init__(self,i)
            obj.append(o)
        return obj

When I execute this on iPython (iPython 0.10 and Python 2.6.6) I get the following

In [466]: l = [1,2]
In [502]: A._multiple(*l)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

TypeError: unbound method _multiple() must be called with A instance as 
first argument (got int instance instead)

I'm definitely unclear about the invocation as well as the 'self' keyword usage. Could you help me out in getting this correct?

Thank you.

like image 340
Shyam Sunder Avatar asked Mar 18 '26 07:03

Shyam Sunder


2 Answers

TypeError: unbound method _multiple() must be called with A instance as first argument (got int instance instead)

The Error is self explanatory. It means that an instance method is being called as a Class method. To make the instance method as a class method add the decorator @classmethod

>>> class A:
    def __init__(self,a):
        self.a = a
    @classmethod
    def _multiple(cls,*l):
        #Create multiple instances of object `A`
        return [A(i) for i in l]

>>> l = [1,2]
>>> A._multiple(*l)
[<__main__.A instance at 0x066FBB20>, <__main__.A instance at 0x03D94580>]
>>> 
like image 148
Abhijit Avatar answered Mar 20 '26 20:03

Abhijit


You want a class method:

class A:
    def __init__(self,a):
        self.a = a

    @classmethod
    def _multiple(cls,*l):
        obj = []
        for i in l:
            o = cls(i)
            obj.append(o)
        return obj


>>> A._multiple(1, 2) # returns 2 A's
[<__main__.A instance at 0x02B7EFA8>, <__main__.A instance at 0x02B7EFD0>]

The classmethod decorator replaces the usual self as the first parameter with a reference to the class (in this case A). Note that doing it this way means if you subclass A and call _multiple on the subclass it will be passed the subclass instead.

class B(A): pass

>>> B._multiple(1, 2, 3)
[<__main__.B instance at 0x02B87C10>, <__main__.B instance at 0x02B87D28>, <__main__.B instance at 0x02B87CD8>]

would create a list of B objects.

like image 27
Duncan Avatar answered Mar 20 '26 20:03

Duncan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!