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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:10:48+00:00 2026-05-23T20:10:48+00:00

I have Python classes with object attributes which are only declared as part of

  • 0

I have Python classes with object attributes which are only declared as part of running the constructor, like so:

class Foo(object):
    def __init__(self, base):
        self.basepath = base

        temp = []
        for run in os.listdir(self.basepath):
            if self.foo(run):
                temp.append(run)
        self.availableruns = tuple(sorted(temp))

If I now use either help(Foo) or attempt to document Foo in Sphinx, the self.basepath and self.availableruns attributes are not shown. That’s a problem for users of our API.

I’ve tried searching for a standard way to ensure that these “dynamically declared” attributes can be found (and preferably docstring’d) by the parser, but no luck so far. Any suggestions? Thanks.

  • 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-05-23T20:10:50+00:00Added an answer on May 23, 2026 at 8:10 pm

    You could define a class variable with the same name as the instance variable. That class variable will then be shadowed by the instance variable when you set it. E.g:

    class Foo(object):
        #: Doc comment for availableruns
        availableruns = ()
    
        def __init__(self, base):
            ...
            self.availableruns = tuple(sorted(temp))
    

    Indeed, if the instance variable has a useful immutable default value (eg None or the empty tuple), then you can save a little memory by just not setting the variable if should have its default value. Of course, this approach won’t work if you’re talking about an instance variable that you might want to delete (e.g., del foo.availableruns)– but I find that’s not a very common case.

    If you’re using sphinx, and have "autoattribute" set, then this should get documented appropriately. Or, depending on the context of what you’re doing, you could just directly use the Sphinx .. py:attribute:: directive.

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

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I have a Python 2.x module.py file that looks like this: class A(object): KEYWORD
I have the following situation in my python code: class Parent(object): def run(self): print
I'm finding myself writing a lot of classes with constructors like this: class MyClass(object):
class Foo(object): pass foo = Foo() def bar(self): print 'bar' Foo.bar = bar foo.bar()
I've got a registry of classes and types in Python 2.5, like so: class
In Python 3.x all classes are subclasses of object . In 2.x you have
I have a base class in C# which is like so Class A {
how do i create object-instances on runtime in python? say i have 2 classes:
I have a series of Python classes in a file. Some classes reference others.

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.