0

I have a certain class and i'm having difficulty setting up the__init__ constructor. I need it to take in zero or more strings as arguments, each giving a city name and state abbreviation, and indicating a destination along a tour of US cities.

For example:

Tour("New York, NY", "Lansing, MI", "Los Angeles, CA")

represents a tour that starts in New York city, proceeds to Lansing, and ends in Los Angeles.

Any ideas how to go about doing this using python 3.3?

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
Tyler
  • 1,933
  • 5
  • 18
  • 23

2 Answers2

2

Like this:

class Tour:
    def __init__(self, *cities):
        # cities is a tuple of arguments, do what you want with it

 

When you call

Tour("New York, NY", "Lansing, MI", "Los Angeles, CA")

cities in __init__ will be set to ("New York, NY", "Lansing, MI", "Los Angeles, CA").

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
1

This may help...

Example

class Bar:
   def __init__ (self, arg1=None, arg2=None, ... argN=None):

class NEW (Bar):    
    def __init__ (self, my_new_arg=None, ??? )
       self.new_arg = my_new_arg
       Bar.__init__(self, ??? )
Dharmjeet Kumar
  • 533
  • 7
  • 23