Dynamic Python Syntax

Posted on 2009-12-27
Last Modified: 2012-05-08
Hello experts,

I am not sure if I it is possible to do what I want to do. Consider the following two lines.

document_type = 'Book'
documents = bookModel.document_type.objects.all()

document_type can be 'Book', 'Magazine', 'Essay' etc.
I don't want to have to write an if sentence for each type like this :
if document_type == 'Book':
    documents = bookModel.Book.objects.all()

Can I somehow make it work like the first two lines of code ?
Question by:mannsi
    LVL 11

    Expert Comment

    Why can't you use the lines you typed?

    document_type = 'Book'
    documents = bookModel.document_type.objects.all()

    Just set documents after you have set the document_type. Or are there any errors with it?

    I would guess you set the document_type somewhere after you have found out which type it is.

    Author Comment

    I get the following error:
    AttributeError: 'module' object has no attribute 'document_type'
    LVL 9

    Accepted Solution

    There is no need for IFs.
    # classes is defined here
    class Book:
    class Magazine:
    class Essay:
    # this is a factory class that creates instances
    class factory:
        def __getitem__(cls, name):
            return globals()[name]
    # then you can use factory class to create objects e.g.
    document_type = 'Book'
    f = factory()
    abook = f[document_type]

    Open in new window

    LVL 11

    Expert Comment

    by:Kusala Wijayasena
    Using "globals()" inbuilt function you can easily do this

    documents  = globals()[document_type]()


    Author Closing Comment

    I am very happy with your answers.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now