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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:24:54+00:00 2026-05-26T23:24:54+00:00

I’ve written some code that allows the term ‘job’ to be used universally to

  • 0

I’ve written some code that allows the term ‘job’ to be used universally to perform a unique task. The specific jobs can be chosen through setting an initial variable “job_type”. From that initial variable a particulay subclass is chosen to perform the appropriate job. Maybe the code will make more sense 🙂

if __name__=='__main__':
    # these variables would normally be called in from a config file
    job_type = 'job1'
    uni_var = 10

    job_select = superClass(job_type, uni_var)
    job_select.job()        


class superClass(object):
    def __init__(self, job_type, uni_var):
        self.job_type = job_type   
        self.uni_var = uni_var

        if self.job_type == 'job1':
            self.jobChoice = option1()
        else:
            self.jobChoice = option2()

    # This is the definition called by the main function it then
    # redirects the request to the appropriate job sub class   
    def job(self):
        self.jobChoice.job()

class option1(superClass):
    def __init__(self):
        pass

    def job(self):
        print 'job option 1'
        print uni_var

class option2(superClass):
    def __init__(self):
        pass

    def job(self):
        print 'job option 2'
        print uni_var

The thought behind this code was to allow a single/constant ‘main’ function, to action a variety of unique tasks based purely on the variable ‘job_type’. Which it seems to be doing fine.

My question (as a very inexperienced coder) is, have I gone about this the right way or is there a better way to do things?

Also, have I set up the variable ‘uni_var’ correctly in the superClass to be correctly shared across all/any superClass subclasses?

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-26T23:24:55+00:00Added an answer on May 26, 2026 at 11:24 pm

    I suspect that what you really want is to use the Factory Method Pattern here.

    You could change your code to something like this:

    if __name__=='__main__':
        # these variables would normally be called in from a config file
        job_type = 'job1'
        uni_var = 10
    
        job_select = superClass.optionFactory(job_type, uni_var)
        job_select.job()        
    
    
    class superClass(object):
        def __init__(self, job_type, uni_var):
            self.job_type = job_type   
            self.uni_var = uni_var
    
        # This is the definition called by the main function it then
        # redirects the request to the appropriate job sub class   
        def job(self):
            raise NotImplementedError()
    
        @staticmethod
        def optionFactory(job_type, uni_var):
            "Return an instance of superClass based on job_type and uni_var."
            if job_type == "job1":
                return option1(job_type, uni_var)
            else:
                return option2(job_type, uni_var)
    
    class option1(superClass):
        def __init__(self, job_type, uni_var):
            super(option1, self).__init__(job_type, uni_var)
    
        def job(self):
            print 'job option 1'
            print uni_var
    
    class option2(superClass):
        def __init__(self, job_type, uni_var):
            super(option2, self).__init__(job_type, uni_var)
    
        def job(self):
            print 'job option 2'
            print uni_var
    

    However, notice that this implementation will require that superClass be changed every time a new subclass is created. Another alternative would be to make the optionFactory method a standalone function (rather than a method of superClass). Like this:

    if __name__=='__main__':
        # these variables would normally be called in from a config file
        job_type = 'job1'
        uni_var = 10
    
        job_select = optionFactory(job_type, uni_var)
        job_select.job()        
    
    
    class superClass(object):
        def __init__(self, job_type, uni_var):
            self.job_type = job_type   
            self.uni_var = uni_var
    
        # This is the definition called by the main function it then
        # redirects the request to the appropriate job sub class   
        def job(self):
            raise NotImplementedError()
    
    class option1(superClass):
        def __init__(self, job_type, uni_var):
            super(option1, self).__init__(job_type, uni_var)
    
        def job(self):
            print 'job option 1'
            print uni_var
    
    class option2(superClass):
        def __init__(self, job_type, uni_var):
            super(option2, self).__init__(job_type, uni_var)
    
        def job(self):
            print 'job option 2'
            print uni_var
    
    def optionFactory(job_type, uni_var):
        "Return an instance of superClass based on job_type and uni_var."
        if job_type == "job1":
            return option1(job_type, uni_var)
        else:
            return option2(job_type, uni_var)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

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.