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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:48:39+00:00 2026-05-31T18:48:39+00:00

Present Scenario : I have a set of classes that all take a common

  • 0

Present Scenario :

I have a set of classes that all take a common argument context in constructor, and all the classes inherit from a common base.

class base:
    def common_method(self):
        pass

class a(base):
    def __init__(self,context, aa):
        pass

class b(base):
    def __init__(self, context, bb):
        pass

# ...

class z(base):
    def __init__(self, context, zz):
        pass

Usage in main:

context = get_context_from_some_method()

A = a(context, 1)
B = b(context, 2)
C = c(context, 3)
D = d(context, 4)
.
.
.
Z = z(context, 26)

Problem:

  • Is there a tricky way possible to supply context to all the classes implicitly ?
  • We have a common base class, but I don’t see a obvious way to let it set the context for all the classes.
  • It can be assumed that I want to keep context common to all my class instances.
  • Just a random thought – Can meta class help somehow ?

I know it seems silly, but I just want to get rid of the redundancy in some way, so that I can set a global context somehow and concentrate on my objects.

Please suggest some way around ?

** Update to Question **

I can-not set a context in the base class, since this is to be used in a web application. So, many pages with different context will be using the class structure. So, If I set a context in base then it will conflict to the context that will be set by another instance of webpage that will use the same base. Since, in a web-application all the above classes will be in memory common to all pages.

  • 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-31T18:48:40+00:00Added an answer on May 31, 2026 at 6:48 pm

    Edit: If you don’t want to / can’t use a class variable, your best bet is to use a factory function. Either make it a static method on the base class, or a module level function.

    def make_instances(context, *instances):
        return [cls(context, *args) for cls, args in instances]
    
    A, B, ..., Z = make_instances(get_context_from_some_method(), 
                     (a, (1,)), (b, (2,)), ..., (z, (26,)))
    
    # or
    instances = make_instances(get_context_from_some_method(), 
                 zip(list_of_subclasses, ((x,) for x in range(1, 27))))
    

    Alternatively, and I don’t know if this works in your situation, just use a module level global variable:

    class z(base):
        def __init__(self, zz):
            self.zz = zz
            self.context = context
    
    context = 'abc'
    Z = z(26)
    

    In addition to the advice to use class variables from the other answer, I advise you to copy the context onto the instances, so that you can later change the context without affecting already created instances.

    class base:
        context = None # if you want to be able to create without context.
        # just omit the previous line if you want an error
        # when you haven't set a context and you try to instantiate a subclass
    
    class a(base):
        def __init__(self, aa):
            self.aa = aa
            self.context = self.context # sets an instance variable
            # so the class variable can be changed
    
    class b(base):
        def __init__(self, bb):
            self.bb = bb
            self.context = self.context
    
    base.context = 'context'
    
    A = a(1)
    B = b(2)
    
    base.context = 'newcontext'
    
    print A.context # context
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario that i suspect is very common, i've found various ideas
Scenario: I have a fairly generic table (Data), that has an identity column. The
This is the scenario i have: im developing a web app that will list
Let's assume a simple and common scenario. I have a User model with an
I've got a scenario where I have Image Sources set to null which are
Scenario: I know nothing about packages except that they contain functions and procedures all
I have scenario where I have to poll documents from sharepoint library based on
Here is my scenario: I have table A, that has 4 rows (id, col1,
At present it seems that VS2008 still isn't supported either in the 5.1.5 release
My present contract engagement is at a large E-Commerce company. Their code base which

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.