Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating objects in python

Tags:

python

I have a python class I want to instantiate and the __init__ definition has a lot of parameters (10+). Is there a clean way to instantiate a class who's __init__ takes a lot of params?

For example:

class A(object):

    def __init__(self, param1, param2, param3,...param13):
    // create an instance of A


my_a = new A(param1="foo", param2="bar", param3="hello"....)

Is there a cleaner way to do this? like passing in a dictionary or something? Or better, is there an expected convention?

like image 965
9-bits Avatar asked Mar 27 '12 19:03

9-bits


People also ask

What is instantiating an object?

An instance of an object can be declared by giving it a unique name that can be used in a program. This process is known as instantiation. A class can also be instantiated to create an object, a concrete instance of the class. The object is an executable file that can run on a computer.

How do you initialize an object in Python?

Python Object Initialization When we create object for a class, the __init__() method is called. We use this to fill in values to attributes when we create a object. Here, __init__() has two attributes apart from 'self'- color and shape. Then, we pass corresponding arguments for these at the time of object creation.

Where can you instantiate an object in Python?

In short, Python's instantiation process starts with a call to the class constructor, which triggers the instance creator, . __new__() , to create a new empty object. The process continues with the instance initializer, . __init__() , which takes the constructor's arguments to initialize the newly created object.

How do we instantiate an object?

When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.


2 Answers

Yes, you can use a dict to collect the parameters:

class A(object):
    def __init__(self, param1, param2, param3):
        print param1, param2, param3


params = {'param1': "foo", 'param2': "bar", 'param3': "hello"}
# no 'new' here, just call the class
my_a = A(**params)

See the unpacking argument lists section of the Python tutorial.

Also, // isn't a comment in Python, it's floor division. # is a comment. For multi-line comments, '''You can use triple single quotes''' or """triple double quotes""".

like image 113
agf Avatar answered Oct 10 '22 13:10

agf


There is no new keyword in Python. You just invoke the class name as you would a function.

You may specify keyword arguments using a dictionary by prefixing the dictionary with **, for example:

options = {
    "param1": "foo",
    "param2": "bar",
    "param3": "baz"
}

my_a = A(**options)

If you're going to be defining all of the values at once, using a dictionary doesn't really give you any advantage over just specifying them directly while using extra whitespace for clairity:

my_a = A(
    param1 = "foo",
    param2 = "bar",
    param3 = "baz"
)
like image 41
Jeremy Avatar answered Oct 10 '22 13:10

Jeremy