When trying to invoke def, I get: parameter 'self' unfilled [closed]
Asked Answered
W

1

22

I'm trying to write a class to randomly pick a number of songs from a dictionary. The whole point of it is to be able to simply type DJ.chooseListing() for example to execute the random selection. Alas it gives me an error I can't really find anywhere else on the web, and I feel the answer is very simple.

class DJ:
     # put song variables here in this format: trackName = Track("string of song to be displayed", 'music/filename.ogg')
     trackRickAstleyNever = Track("Never gonna give you up", 'music/rick.ogg')
     trackShootingStars = Track("Shooting Stars", 'music/shoot.ogg')

     dictSongs = {
         # put songs here like this: 'name': varName,
         'nevergonnagiveyouup': trackRickAstleyNever,
         'shootingstars': trackShootingStars,

     }
     """
     arrayRandomized = [
         # Put future songs here in this format: songs['name'],
     ]
     """
     arrayChosen = []
     trackChosen = ""

     def chooseListing(self):
         for i in xrange(4):
             self.arrayChosen.append(random.choice(self.dictSongs))

     def chooseTrack(self):
         self.trackChosen = random.choice(self.arrayChosen)

DJ.chooseListing()
Whimsical answered 23/2, 2017 at 20:47 Comment(6)
What is bad about it? I just use tabs?Whimsical
What you have posted here will give an IndentationError. Unless that's the problem you're asking about, you need to fix it.Aloysia
My code works completely fine, I use pycharm and I've gotten no indentation issues so far. Can you please give me an example of what I'm doing wrong then?Whimsical
Your code may work fine, but the code you've posted here would not. The content of a class has to be indented below the class XX: line. Please make sure that if you're pasting Python code you reproduce your indentation accurately, otherwise you are introducing new errors into the code you are asking people to read.Aloysia
Oh i s?, It's a copy & paste error, i'll update that asapWhimsical
I fixed it now, can someone please explain to me still how to do this? I'm very new to Stackexchange and now my post has been put on hold, I don't understand what's so off-topic about it...Whimsical
H
41

A function defined in a class is a method. A method's first argument will be filled automatically when you call it on an instance of the class. You're defining a chooseListing method, but when you call it, you're not creating an instance. If there's no instance, there's nothing that can be passed as self, which leads to your exception. Try:

dj = DJ()
dj.chooseListing()

You may still need to make some other fixes to your class though, since currently all of its attributes are class attributes (which are shared by all instances). If you create mutltiple DJ instances, they will all share the same arrayChosen list, which is probably not what you intend. You should move your assignments to an __init__ method, where you can make them instance attributes (by assigning on self):

def __init__(self):
    self.dictSongs = {  # this might still make sense as a class attribute if it is constant
        'nevergonnagiveyouup': trackRickAstleyNever,
        'shootingstars': trackShootingStars,
        }
    self.arrayChosen = [] # these however should certainly be instance attributes
    self.trackChosen = ""

I left the Track instances out of the __init__ method, but that might not be correct if they have internal state that shouldn't be shared between DJs.

Howzell answered 23/2, 2017 at 21:23 Comment(3)
I tried doing what you did here previously, also the point is to only make one instance in total. I'll try this again tomorrow, but I don't think it will work as it didn't last time..Whimsical
Oh hey that actually worked, i don't see what i did wrong the first time i tried it, anyways, thanks for the help!!Whimsical
necro: don't forget that when creating an object, you call a constructor, so there must be parentheses! gave me a headache to find this problem...Buskirk

© 2022 - 2024 — McMap. All rights reserved.