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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:03:26+00:00 2026-05-19T15:03:26+00:00

I was wondering if there is anything wrong (from a OOP point of view)

  • 0

I was wondering if there is anything wrong (from a OOP point of view) in doing something like this:

class Foobar:
    foobars = {}
    def __init__(self, name, something):
        self.name = name
        self.something = something

        Foobar.foobars[name] = self

Foobar('first', 42)
Foobar('second', 77)

for name in Foobar.foobars:
    print name, Foobar.foobars[name]

EDIT: this is the actual piece of code I’m using right now

from threading import Event
class Task:
    ADDED, WAITING_FOR_DEPS, READY, IN_EXECUTION, DONE = range(5)
    tasks = {}
    def __init__(self, name, dep_names, job, ins, outs, uptodate, where):
        self.name = name
        self.dep_names = [dep_names] if isinstance(dep_names, str) else dep_names
        self.job = job
        self.where = where
        self.done = Event()
        self.status = Task.ADDED
        self.jobs = []
        # other stuff...
        Task.tasks[name] = self
    def set_done(self):
        self.done.set()
        self.status = Task.DONE
    def wait_for_deps(self):
        self.status = Task.WAITING_FOR_DEPS
        for dep_name in self.dep_names:
            Task.tasks[dep_name].done.wait()
        self.status = Task.READY
    def add_jobs_to_queues(self):
        jobs = self.jobs
        # a lot of stuff I trimmed here
        for w in self.where: Queue.queues[w].put(jobs)
        self.status = Task.IN_EXECUTION
    def wait_for_jobs(self):
        for j in self.jobs: j.wait()
    #[...]

As you can see I need to access the dictionary with all the instances in
the wait_for_deps method. Would it make more sense to have a global variable
instead of a class field? I could be using a wrong approach here, maybe that
stuff shouldn’t even be in a method, but it made sense to me (I’m new to OOP)

  • 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-19T15:03:26+00:00Added an answer on May 19, 2026 at 3:03 pm

    Yes. It’s bad. It conflates the instance with the collection of instances.

    Collections are one thing.

    The instances which are collected are unrelated.

    Also, class-level variables which get updated confuse some of us. Yes, we can eventually reason on what’s going on, but the Standard Expectation™ is that state change applies to objects, not classes.


     class Foobar_Collection( dict ):
         def __init__( self, *arg, **kw ):
             super( Foobar_Collection, self ).__init__( *arg, **kw ):
         def foobar( self, *arg, **kw ):
             fb= Foobar( *arg, **kw )
             self[fb.name]= fb
             return fb
    
     class Foobar( object ):
         def __init__( self, name, something )
             self.name= name
             self.something= something
    
    fc= Foobar_Collection()
    fc.foobar( 'first', 42 )
    fc.foobar( 'second', 77 ) 
    
    for name in fc:
        print name, fc[name]
    

    That’s more typical.


    In your example, the wait_for_deps is simply a method of the task collection, not the individual task. You don’t need globals.

    You need to refactor.

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

Sidebar

Related Questions

i just wondering is there any performance issue or anything thing wrong if that
Is there anything wrong or unhealthy about creating a subview of the view controller's
I was wondering if there is anything wrong with having two classes with the
I was wondering whether there is anything wrong with passing a pointer to putchar,
Wondering if there is any way to get the lambda expressions that result from
I am wondering if there is any potential security risk from the following code.
There may not be any real world use for this but i was wondering
On iOS, I am wondering if there is anything akin to the NSZombies flag
This is going to sound like a dumb question, but I was wondering where
I was wondering, is there anything in the RTTI of Delphi that will do

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.