1

I am trying to implement a project using class inheritance, where parent class is:

class radio:

    def __init__(self, artist=None, track=None, genre=None):

        self.artist = artist 
        self.track = track
        self.genre = genre

then I create methods for each attribute (non working examples):

    def seed_artist(self):
        results = api.search(q=self.artist, type='artist')
        return results 

    def seed_track(self):
        results = api.search(q=self.track, type='track')
        return results

    def seed_genre(self):
        results = api.search(q=self.genre, type='genre')
        return results

the user is going to pick a seed above (only one), being it either artist, track, genre or mood, and that's why I initialize all arguments with None, leaving the argument value to be inserted at inheritance level.

the inherited class is:

class playlist(radio):

    def __init__(self,user):
        radio.__init__(self, artist, track, genre)

lets say user inputs at command line a track seed, and my script ends up starting with a global variable:

track = 'karma police'

this way, all other attributes (artist, genre) remain None.

when I create an instance, say:

jeff = playlist('Jeff Smith')

It will throw an error saying that genre and artist are not defined, and I would have to inherit only trackattribute, like so:

radio.__init__(self, track)

I know I could start the script with global variables defined:

track = None
genre = None
artist = None

and this would allow me to have:

radio.__init__(self, artist, track, genre)

but this seems to me rather redundant...

Is there a workaround this, with no need for initial values of global variables set to None, keeping all the code within class scope definitions?

2
  • I gave an answer below, but it's hard to say what the alternative for the global variables is, because you haven't shown or even described how your code gets from user input to creating a global variable called tracks. One possibility is to make playlist also accept track, artist, etc., and have your input-parsing code pass a dict to it in a manner similar to what I described in my answer. Commented Dec 31, 2016 at 19:11
  • @BrenBarn I've edited the question for you, in case you might want to edit your answer. Commented Dec 31, 2016 at 19:20

2 Answers 2

1

Instead of using a separate global variable for each field, use a single dictionary for all fields. Then you can pass the dictionary as keyword arguments.

That is, instead of creating a global variable called artist, create a dictionary called radio_fields or something. When the user inputs their query, set a key on this dict:

radio_fields = {}
...
# when you get the user's command
radio_fields['track'] = 'Karma Police'

Now you can call radio.__init__(self, **radio_fields). This will pass as arguments whatever keys are in radio_fields. If there is only one, only that one will be passed, and the others will take their default values as you defined them when you defined radio.__init__.

There might be a better way to structure your overall program, but it's hard to say based on what you've shown here. For instance, it's unclear why playlist only accepts one user argument. If playlist accepted all the field arguments that radio accepted, it could pass them on to radio without any problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have those variables to pass, so don't try and pass them. The parent class already has defaults for them, so just pass the one you need:

radio.__init__(self, artist=artist)

2 Comments

The problem is then he would have to write a bunch of separate calls depending on which variables are defined, along with a tangle of if statements to decide which call to use.
but I don't know the one I need... That's the user's call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.