0

I'm very much a noob in programming, and currently world-building for a Sci-Fi based TTRPG. I'm trying to automate generation of various things in my world such as planets etc. One of these is ship generation. I created a class for my ship:

class starShip:
    def __init__(self, cost, speed, armour):
        self.cost = cost
        self.speed = speed
        self.armour = armour

And a couple of instances:

shuttle = starShip(200000,3,0)
fighter = starShip(500000,5,5)

I'm using pandas and a csv file to randomise the ships generated. For example:

def shipGenerator(rowNum):
    for col in dfShips.columns:
       if col == "Ship Type": #need to find relevant column header in the original data frame
       result = randint(1,20) #give random number
       shipType = dfShips.loc[result,"Ship Type"] #get the value from relevant cell in original data frame
       dfGeneratedShips.at[rowNum,col] = shipType #generate the ship type in a new dataframe

Now I can do a lot of if/elif for each type of ship- if shuttle, then put shuttle.cost (instance of the starShip class) in the relevant cost cell etc. etc. But with 12 different types of ships, that's a lot of if/elif, and I'm trying to do this right even though it's a fun project and I'm a complete noob. So my question, is there a way do use a string variable to call an instance? For example, the variable shipType now contains "shuttle" or "fighter". Is there a way to call an instance of starShip class using that variable? Like:

dfGeneratedShips.loc[rowNum,"Cost"] = shipType.cost #place the cost of the relevant ship type under the appropriate column in new dataframe
dfGeneratedShips.loc[rowNum,"Speed"] = shipType.speed

But shipType refers to "shuttle" or "fighter" or "carrier" at each iteration of the loop, so the result changes appropriately.

I hope this was clear enough, appreciate the help!

BoazP
  • 1
  • You should probably have `ships['shuttle']` and `ships['fighter']` – Charles Duffy Dec 02 '21 at 20:54
  • 1
    Terminology note: you don't mean the word "call", you mean "reference". e.g. "can I reference an object using a string". Call means something like "call a function" (or any "callable") E.g. `myfunction()` – juanpa.arrivillaga Dec 02 '21 at 21:00
  • Thank you for letting me know- as said, very much a newbie. This also must be why googling this issue didn't work- I used the wrong term! =/ – BoazP Dec 02 '21 at 21:19

1 Answers1

0

A dictionary could do it..

with

ships = {"shuttle":starShip(200000,3,0),"fighter":starShip(500000,5,5)}  

you can do

ships["shuttle"].cost
ships["shuttle"].speed
JeffUK
  • 4,107
  • 2
  • 20
  • 34
  • BTW, this is something of a FAQ. (You answered just as I was getting ready to find a duplicate to close it with). – Charles Duffy Dec 02 '21 at 20:54
  • 1
    This is brilliant stuff, thank you so much! – BoazP Dec 02 '21 at 20:58
  • @CharlesDuffy I think my answer is more useful than the 'How do I create Variable variables' which requires OP to make the connection that instances are variables too without being explicit. – JeffUK Dec 02 '21 at 21:58
  • @JeffUK, sure -- I sometimes write a community-wiki answer (flagged CW to not be getting rep points for answering a known duplicate, and to make it clear others are welcome to edit) showing how to apply a technique in an OP's specific scenario when closing something as a duplicate myself. But that doesn't mean it's not a duplicate, and the accepted answer in the linked duplicate _does_ very much show this technique. – Charles Duffy Dec 02 '21 at 22:14
  • @CharlesDuffy That sounds like helping OP by answering their question.. only with lots of extra steps! – JeffUK Dec 02 '21 at 22:21
  • @JeffUK, ...the thing here is that we're not here to help individual OPs -- we're here to build a knowledgebase, and a knowledgebase that has fewer, more canonical questions with better-vetted answers is a better one than one where you've got 20 copies of the same question and answers are spread out between them with varying levels of quality (and considerably less editorial review, comments digging into caveats on individual approaches, etc etc). The goal is to be a good enough resource that the next person with the same problem doesn't _have_ to ask a new question. – Charles Duffy Dec 02 '21 at 22:25
  • Well I'm here to help individual OPs! "Stack Overflow is a question and answer site" not a knowledgebase. – JeffUK Dec 02 '21 at 22:39