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 8540819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:41:20+00:00 2026-06-11T11:41:20+00:00

Not sure if this is possible in Python, but I’d like to have an

  • 0

Not sure if this is possible in Python, but I’d like to have an instance of a class as a module-level global variable that, when called, returns a default property.

The idea comes from this question that overrides __call__ to have a class mimic a function.

Quick (untested) example of the idea:

class _foo(object):
    __slots__ = ("_foo",)
    _init = False

    def __init__(self):
        if not _init:
            self._foo = ["bar", "baz", "zog"]

    def __call__(self, item=None):
        if itme is not None:
            return self._foo[item]
        else:
            return self._foo

    def __getitem__(self, item):
        return self._foo[item]

# Module level
FOO = _foo()

Assume the above code is in a module (foo.py). Using the interpreter, I cal get the callable form to work how I want, but not the non-callable form.

>>> import foo

# callable forms
>>> foo.FOO(0)
"bar"
>>> foo.FOO()
["bar", "baz", "zog"]

# attribute forms
>>> foo.FOO[2]
"zog"

# doesn't work
>>> foo.FOO
<foo._foo object at 0xdeadbeef>

# desired output
>>> foo.FOO
["bar", "baz", "zog"]

My thinking is that I need to define a default property somehow so that when calling the instance directly, I get the internal list/dict of my choosing. I could cheat and do this via overriding repr, but that kinda breaks Python’s own API and I want to avoid that. Passing no value in square brackets (>>> foo.FOO[]) yields a syntax error instead.

I’d like any potential solutions to be compatible with Python 2.4 at minimum and 2.7 at maximum. If this isn’t possible, then I guess I’ll just stick to using the callable format.

  • 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-11T11:41:20+00:00Added an answer on June 11, 2026 at 11:41 am

    This is impossible, sorry. You can’t have foo.FOO be both a callable (a _foo instance) and an unadulterated list object.

    Of course, I did say “unadulterated”. If you’re feeling sneaky…

    class _foo(list):
        _init = False
        def __init__(self):
            if self._init:
                list.__init__(self)
            else:
                list.__init__(self, ['bar', 'baz', 'zog'])
    
        def __call__(self, item=None):
            if item is None:
                return self
            return self[item]
    
    FOO = _foo()
    

    Usage:

    >>> FOO()
    ['bar', 'baz', 'zog']
    >>> FOO(0)
    'bar'
    >>> FOO
    ['bar', 'baz', 'zog']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if something like this is even possible in Python, but if
I'm not sure if this is possible (complete non-flash developer speaking), but we have
Not sure this is possible, but looking to write a script that would return
I'm not sure if this possible but probably is fairly simple. I've made a
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
Not sure if this is possible in one Makefile alone, but I was hoping
I'm not sure this is even possible but I know if it is, the
I'm not sure if this is possible but I want to see objects created
I'm not sure this is possible, but I'm interesting in making this happen. Ideally,

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.