CGul CGul - 8 months ago 14
Python Question

How do you name a new instance of a class when you don't know what the name is in advance?

I'm scraping some pages to gather data on athletes. I start off like so:

req = requests.get(url)
soup = BeautifulSoup(req.text, "html.parser")

and I go on to get things like the athlete's name, team, height, weight, etc.

Once I've finished gathering the data, I want to create an instance of my athlete class. Obviously, when testing, I can just do something like this:

david_ortiz = athlete.Athlete(name, team, height, weight)

My problem is that I don't know how to name these new instances (outside of when I'm making instances like david_ortiz for testing). How do I name these instances without hardcoding the athletes name in advance? It's also possible that there could be multiple athletes with the same name so maybe this wouldn't be the best solution? If that's the case, what do people do in this situation?


I think you can use dict to store name you get from web data

allAthlete = { name : athlete.Athlete(name, team, height, weight) }


allAthlete = {}
allAthlete[name] = athlete.Athlete(name, team, height, weight)

when you want to access the data in dict:


to call method of athlete: