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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:16:03+00:00 2026-06-15T17:16:03+00:00

I’m trying to figure out some OOP stuff with python and I’m running into

  • 0

I’m trying to figure out some OOP stuff with python and I’m running into a few problems. I’m trying to make a “game” that involves the player walking around a grid of rooms, with each room an instance of the Room class. If I wanted to make a big grid, instantiating each room would be a pain as I could potentially have to type in the same repetitive coordinate pattern for 64 different rooms, so I wanted to make a function that would do it for me, I’m running into problems figuring out how. Here’s the code I have:

class Room(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

def generate_rooms():

    names = [a,b,c,d]
    locations = [[1,1],[1,2],[2,1],[2,2]] #this line could be a few for loops

    for x in range(0,4):
        names[x] = Room(locations[x][0],locations[x][1])

The idea was that this would create 4 Rooms named a, b, c, and d with the coordinates specified in locations. Python won’t let me do this because a, b, c, and d aren’t defined. In any implementation I’ve tried I’ve run into the problem that naming instances needs to be done with variable names, and I wouldn’t know how to generate those dynamically.

I’ve searched around a lot and it doesn’t really seem like automation of instantiation is really something people would want to do, which confuses me because it seems like it would really make sense in situations like this.

Any help on how to fix this or on how to accomplish this task in a better way is greatly appreciated!

  • 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-15T17:16:04+00:00Added an answer on June 15, 2026 at 5:16 pm

    You’re very close! The usual approach is to use a dictionary, and to use the names you want as dictionary keys. For example:

    >>> class Room(object):
    ...     def __init__(self, x, y):
    ...         self.x = x
    ...         self.y = y
    ...         
    >>> rooms = {}
    >>> names = ['a', 'b', 'c', 'd']
    >>> locations = [[1,1], [1,2], [2,1], [2,2]]
    >>> for name, loc in zip(names, locations):
    ...     rooms[name] = Room(*loc)
    ...     
    >>> rooms
    {'a': <__main__.Room object at 0x8a0030c>, 'c': <__main__.Room object at 0x89b01cc>, 'b': <__main__.Room object at 0x89b074c>, 'd': <__main__.Room object at 0x89b02ec>}
    >>> rooms['c']
    <__main__.Room object at 0x89b01cc>
    >>> rooms['c'].x
    2
    >>> rooms['c'].y
    1
    

    This way you can iterate over the rooms in several ways, for example:

    >>> for roomname, room in rooms.items():
    ...     print roomname, room.x, room.y
    ...     
    a 1 1
    c 2 1
    b 1 2
    d 2 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am currently running into a problem where an element is coming back from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is

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.