When trying to invoke def, I get: parameter 'self' unfilled – Self

Photo of author
Written By M Ibrahim
abstract-class for-loop python-2.7 self-signed

Quick Fix: In a class, functions are methods. The first argument of a method is automatically filled when called on an instance of the class. You’re calling chooseListing without creating an instance. Create an instance, then call the method on that instance.

The Problem:

When attempting to utilize the ‘chooseListing’ method of the ‘DJ’ class, I encounter the error: ‘parameter ‘self‘ unfilled’. What is the cause of this issue, and how can it be resolved to enable the execution of ‘chooseListing’ without encountering this error?

The Solutions:

Solution 1: Fix method invocation

To resolve the error “parameter ‘self’ unfilled”, the issue lies in how you’re calling the method `chooseListing`. Methods defined in a class are called on instances of the class. In your code, you’re calling `DJ.chooseListing()` without creating an instance of the `DJ` class. To call a method on an instance, you need to create an instance first.

Here’s the corrected code:

dj = DJ()
dj.chooseListing()

By creating an instance of the `DJ` class and then calling `chooseListing` on it, you provide the necessary `self` parameter. This will allow the method to be executed successfully.

Additionally, the `Track` instances are currently assigned directly to the class. This means they are shared among all instances of the `DJ` class. To make them specific to each instance, you should move their assignment to the `__init__` method, where instance attributes are usually defined.

Here’s the modified `__init__` method:

def __init__(self):
    self.dictSongs = {
        'nevergonnagiveyouup': trackRickAstleyNever,
        'shootingstars': trackShootingStars,
    }
    self.arrayChosen = []
    self.trackChosen = ""

With these changes, the code should work as intended, and you’ll be able to call `DJ.chooseListing()` and `DJ.chooseTrack()` without encountering the error.