Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:59:38+00:00 2026-06-18T03:59:38+00:00

The question Are there any valid use cases for the ol’ module switcheroo, where

  • 0

The question

Are there any valid use cases for the ol’ module switcheroo, where you replace the module with a class instance? By a valid use case, I mean a case where it would be generally agreed that using this trick would be the best way of solving a problem. For example, the module:

VERSION = (1, 2, 8)
VERSION_NAME = '1.2.8'

Could be converted to this:

import sys

class ConstantsModule(object):
    def __init__(self):
        self.VERSION = (1, 2, 8)

    @property
    def VERSION_NAME(self):
        return u'{}.{}.{}'.format(*self.VERSION)

sys.modules[__name__] = ConstantsModule()

And now VERSION_NAME is a property with logic behind it.

I have googled around for this without finding anything relevant. I learned of this trick in a SO answer I read some time ago, and I know this is something referred to as “black magic” and to be avoided, but I’m curious about the valid use cases.


My specific use case

I have a small problem with one of my modules that could easily be solved if the module was a class instance. I have a “constant” called VERSION_NAME, which is a string version of VERSION, which in turn is a tuple with my application’s version information. The VERSION_NAME is used throughout my project and in several other projects based on this one. Now I would like VERSION_NAME to include some logic – I would like it to be based on VERSION so that I don’t have to edit it manually all the time, and I would like it to be formatted slightly differently depending on a couple of environmental circumstances. The way I see it I have two choices:

  • Hunt down every use-case of VERSION_NAME in my project and all its sub-projects and change it to a function call like get_version_name.
  • Invoke black magic like shown above.

This question is not about my use case though, this is just an example of what I figure it could be used for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:59:40+00:00Added an answer on June 18, 2026 at 3:59 am

    Since everything in Python is an object, there is no black magic about it; this is simply duck typing; if you create an object that walks and talks like a module, then the rest of Python is none the wiser.

    However, for your specific use-case, you don’t need to resort to this level of deception. Simply calculate version name at import time:

    VERSION_NAME = u'{}.{}.{}'.format(*VERSION)
    

    Nowhere is it stated that module globals can only be literal values; just use a Python expression for them instead.

    After all, your VERSION_NAME variable is not going to change during the lifetime of your program, you only need to generate it once. Use a property only when you need an attribute that needs to be re-calculated every time you access it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

there's a question: Is there any way to pair Bluetooth device in Windows programmatically?
This is a simple question : Is there any automatic indentation alignment on Eclipse
Simple question here: is there any way to convert from a jagged array to
Just a curious question but is there any programs that can help/aid you when
Is there any difference between ReferencePath with condition '%(CopyLocal)'=='true' and ReferenceCopyLocalPaths ? The question
(See question below for more context): Are there any situations in which <machineKey validationKey=AutoGenerate,IsolateApps
My question is very simple: Is there any solution to install xCode, or equivalent,
My question is very simple: is there any way to (programmatically, technically, or manually)
Update: This question is a duplicate of Are there any programming languages targeting PHP,
Simple question. Are there any tools for generating Xcode projects from the command line?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.