Q:

Creating a list of objects in Python constructor

I would like to pass a variable into the constructor of an object and depending upon the value get one or more more objects out. Something like:
class MyClass():

    def __init__(self,n_objects):

        #Should return n_objects of MyClass

Something doesn't seem quite right about it though since I need to return a list (I think) but self is of type "MyClass". What am I missing?

A:

A constructor only returns a single reference of what you are trying to create, so you can't do it this way.  What you need is to make this list external from this:
result = [MyClass() for i in xrange(n_objects)]

Then make a wrapper function around that.
Do note that xrange is for python 2, it was renamed to range in python 3.
If you must insist on keeping the "encapsulation" of the class, you can just define a classmethod.  Naive example:
>>> class Foo(object):
...     def __init__(self):
...         self.bar = 1
...     @classmethod
...     def Copies(cls, n):
...         return [cls() for i in xrange(n)]
... 
>>> Foo()
<__main__.Foo object at 0x7f2059ac1910>
>>> Foo.Copies(3)
[<__main__.Foo object at 0x7f2059ac19d0>, <__main__.Foo object at 0x7f2059ac1990>, <__main__.Foo object at 0x7f2059ac1a10>]

