KexAri
KexAri

Reputation: 3977

Python dictionary with init objects

I am following a tutorial on Python flask. I have a config.py like so:

import os
basedir = os.path.abspath(os.path.dirname(__file__))


class Config():
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'DDFHSJ734H927YF9843'
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    MAIL_SERVER = 'smtp.googlemail.com'
    MAIL_PORT = 587
    MAIL_USE_TLS = True
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
    FLASKY_MAIL_SENDER = 'Flasky Admin <[email protected]>'
    FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')

    # Implement this later
    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')


class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data-test.sqlite')


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data.sqlite')


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,

    'default': DevelopmentConfig
}

I am confused about the config dictionary at the bottom. For example does 'development': DevelopmentConfig point to an initialized DevelopmentConfig object? Why is it not 'development': DevelopmentConfig(). Also why has object been omit from the arguments in the base Config class?

Upvotes: 0

Views: 91

Answers (2)

Jenner Felton
Jenner Felton

Reputation: 807

In Python, classes are objects. So the development key in that dictionary has a value that is the class DevelopmentConfig. That way, you can call config['development'](), and you will have an initialized DeveloptmentConfig object. As for object being omitted, in Python 3, if no parent class in given to a new class, it's automatically subclasses object.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799450

For example does 'development': DevelopmentConfig point to an initialized DevelopmentConfig object?

No, it holds a reference to the DevelopmentConfig class.

Why is it not 'development': DevelopmentConfig().

Because this may not be the appropriate place to instantiate the class. Moreso if only one of the classes can actually be instantiated without causing a bug.

Also why has object been omit from the arguments in the base Config class?

Because it's purely optional. In Python 2.6+ it creates an old-style class instead of a new-style class, and in Python 3.x it makes no difference (and note that an empty superclass specifier is invalid syntax in 2.5 or older; the parens should be omitted entirely).

Upvotes: 2

Related Questions