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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:11:40+00:00 2026-06-18T05:11:40+00:00

I have some code for solving a puzzle game called nurikabe, recently I’ve been

  • 0

I have some code for solving a puzzle game called nurikabe, recently I’ve been rewriting it to OOP (still learning) and have the following structure:

# CNurikabe.py
from includes import Board, Validation, Heuristics
class CNurikabe(object):
    ...

# CValidation.py
from includes import Board, Heuristics
class CValidation(object):
    ...

# CHeuristics.py
from includes import Board
class CHeuristics(object):
    ...

# CBoard.py
class CBoard(object):
    def __init__(self, filename):
        # Vars shared by every class
        self.x, self.y, self.z, self.t = self.parseData(filename)

# run.py
from CNurikabe import CNurikabe
nurikabe = CNurikabe()
nurikabe.solve('output')

# includes.py
from CBoard import CBoard   
Board = CBoard('data.dat')

from CHeuristics import CHeuristics
Heuristics = CHeuristics()

from CValidation import CValidation
Validation = CValidation()

CBoard class has info which has to be shared among all the other classes (such as board dimensions, number coordinates, etc), also I want it to be instantiated once, if possible preventing dependency injection (unnecessarily passing the filename to each class init method, for example)

The classes are needed to access the following:

CValidation class use: CBoard and CHeuristics

CHeuristics class use: CBoard

CNurikabe class use: CBoard, CValidation and CHeuristics

The code I have, works just as expected. I can call other class’ methods within the other classes just the way I want it, for example:

# CNurikabe.py:
class CNurikabe(object):
    def someFunc(self):
        for i in range(Board.dimensionx):
            Heuristics.doSomeStuff()
            Validation.doSomeMore()

But I’ve read maybe too much about how globals are evil. Also the code inside includes.py is a bit hackish, because if I change the order of the imports the program won’t run, complaining about being unable to import some names.

I also tried another way, only instantiating globally the CBoard class and then, for the other classes, creating an instance of the classes I need. But I felt that was kinda repetitive, creating an unique global instance of CHeuristics inside each class, for example, and that still wouldn’t solve the CBoard global problem.

I also thought about creating an instance inside each class’s init, but then the code would be very verbose, having to call for example: self.Heuristics.doSomeStuff()

So my question is what would be a better approach to structure this? I’ve read about singleton patterns (which may be overkill, since it’s a small project), and endless ways of doing it for multiple languages like C++ and PHP. Actually The way I’m doing it resembles that of the “extern Class instance;” way of doing it in C++, long time ago I was working on a C++ project that had that style and I liked it, didn’t see any problems with it, although the class instances were global.

  • 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-18T05:11:42+00:00Added an answer on June 18, 2026 at 5:11 am

    Globals are evil. However, you probably need a singleton pattern that encapsulate some things together. My experience from C++ and Python is that you can nicely use the hybrid character of the language and use a module in the role of singleton. (If you think more about it, module variables play the role of a singleton member variables, plain functions in the module resemble methods of a singleton.)

    This way I suggest to put the board functionality into board.py heuristic functionality into heuristic.py,…, convert the methods to functions, self.variable to variable and use:

    import board
    import heuristic
    import validation
    
    ...
    
    class CNurikabe:     # the explicit (object) base class is not needed in Python 3
        def someFunc(self):
            for i in range(board.dimensionx):
                heuristics.doSomeStuff()
                validation.doSomeMore()
    

    Think about the import board as about getting the reference to the singleton instance — which actually is, because there is a single instance of the module object. And it will be syntactically the same as your older code — except getting the instance (the module) will be easier.

    Update: Once you need more instances, you should think in classes again. However, passing objects is very cheap operation in Python — you only copy reference values (technically the address, i.e. 4 or 8 bytes). Once you get the reference value, you can easily assign it to a local variable. (Every assignment in Python means copying the reference value, hence sharing the access to the assigned object. This way, there is actually no need for global variables, and no excuse to use global variables.

    Using local variables, the syntax again remains the same.

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

Sidebar

Related Questions

I have some code on two systems running kernel 2.4.20 and kernel 2.4.38 .
I have some code that will change the background color of a specific label
I have some code here that uses bitsets to store many 1 bit values
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that causes the box2d physics simulation to stutter forever after
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have some code that runs a model in a loop. Each iteration of
I have some code that shows results in a table Right now it will
i have some code using jquery mobile + phonegap and run it on my

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.