Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: constructor argument notation

Tags:

python

pyramid

I'm a few months into learning python. After going through pyramid tutorials I'm having trouble understanding a line in the init.py

from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from .models import (
    DBSession,
    Base,
    )



def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

I'm lost about settings=settings in the configurator argument.

What is this telling python?

like image 539
BScott Avatar asked Dec 14 '15 22:12

BScott


People also ask

What is a constructor argument in Python?

Python Constructor. A constructor is a special type of method (function) which is used to initialize the instance members of the class. In C++ or Java, the constructor has the same name as its class, but it treats constructor differently in Python. It is used to create an object.

Is __ init __ constructor?

"__init__" is a reserved method in python classes. It is known as a constructor in OOP concepts. This method called when an object is created from the class and it allows the class to initialize the attributes of a class.

What is __ str __ in Python?

Python __str__()This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object.

How do you pass a constructor as a parameter in Python?

If you want to pass the class, you would pass Foo . Just past the class, bar(Foo) , then local_class = class_name() would create an instance of Foo.


1 Answers

Python functions support keyword arguments:

def add(a, b):
    return a + b

add(a=1, b=2)

This happens here.

 Configurator(settings=settings)

The first settings is the name of the parameter in the __init__ of Configurator. The second is a name for an object in the current name space.

like image 99
Mike Müller Avatar answered Oct 09 '22 07:10

Mike Müller