Is it necessary to make 50 diffrent classes for each state in the USA? Is it necessary to make 50 diffrent classes for each state in the USA? tkinter tkinter

Is it necessary to make 50 diffrent classes for each state in the USA?


As far as I can tell from your posted example code, your requirement is to repeatedly provide a random ("City ST", zip) tuple from a pre-defined set of such values.

This doesn't even need a single class, since it doesn't do any class things. (Here's a bonus video on not writing classes: Stop Writing Classes.)

Just make a big list of tuples, and use random.choice (or .shuffle if that makes more sense for your overall application) to do the job:

_places = []_places.extend((("Albany NY", zip) for zip in (12084, 12201, 12202, 12203, 12204))_places.extend((("Bronx NY", zip) for zip in (10453, 10457, 10460, 10451, 10474))_places.extend((("Buffalo NY", zip) for zip in (14201,14202, 14203, 14204, 14205))# Many, many lines omitted...def random_place():    return random.choice(_places)def update_entry():    locationstate_entry.delete(0, tk.END)    locationstate_entry.insert(0, random_place())


Instead of using a different class for every state, make a dictionary that holds all of your information. Then you can generate your strings based on that and hard-code less information as well.

>>> import random>>> states = {...   'NY': {...     'Albany': [12084, 12201, 12202, 12203, 12204],...     'Bronx': [10453, 10457, 10460, 10451, 10474],...     'Buffalo': [14201,14202, 14203, 14204, 14205]...   }... }>>> state = random.choice(list(states.keys()))>>> state'NY'>>> cities = states[state]>>> cities{'Albany': [12084, 12201, 12202, 12203, 12204], 'Bronx': [10453, 10457, 10460, 10451, 10474], 'Buffalo': [14201, 14202, 14203, 14204, 14205]}>>> city = random.choice(list(cities.keys()))>>> city'Bronx'>>> zips = cities[city]>>> zips[10453, 10457, 10460, 10451, 10474]>>> zip_code = random.choice(zips)>>> zip_code10460>>> city, state, zip_code('Bronx', 'NY', 10460)

If it was my code, I would write a function for this (with a helper function):

>>> def choose_from(dictionary):...     key = random.choice(list(dictionary.keys()))...     return key, dictionary[key]... >>> def random_city_state_zip():...     state, cities = choose_from(states)...     city, zip_codes = choose_from(cities)...     zip_code = random.choice(zip_codes)...     return state, city, zip_code... >>> random_city_state_zip()('NY', 'Albany', 12203)>>> random_city_state_zip()('NY', 'Bronx', 10457)


Since every state behaves the same as every other state, and every city behaves the same as every other city, the only real difference is the data unique to each city or state (names, zip codes).

You can create a single class which takes arguments that define the things unique to a state, such as the name and the abbreviation. The cities can be an attribute of the state which are either added before, after, or during the creation of the state.

For example:

new_york = State("New York", "NY")new_york.add_city("Albany", zips=[12084, 12201, 12202, 12203, 12204])new_york.add_city("Bronx", [0453, 10457, 10460, 10451, 10474])

or this:

albany = City("Albany", zips=[12084, 12201, 12202, 12203, 12204])bronx = City("Bronx", zips=[0453, 10457, 10460, 10451, 10474])new_york = State("New York", "NY", cities=[albany, bronx])

or combine them with this:

new_york = State("New York", "NY", cities = [    City("Albany", zips=[12084, 12201, 12202, 12203, 12204]),    City("Bronx", zips=[0453, 10457, 10460, 10451, 10474]),])

You can then add your random choice methods in a generic way, because they just have to pull from self.zips for a city, or self.cities for a state. Or, you can iterate over every state, and/or every city within a state. It's all quite easy, and just depends on what you need to do.

The point being, create generic state and/or city classes, and generic methods that choose from the unique data for each instance.