flowchart LR call["Workshop(title='Python',<br/>level='beginner')"] --> create["Python creates<br/>a Workshop instance"] create --> bind["Call __init__"] instance["automatic<br/>new instance"] --> self["self"] title_value["'Python'"] --> title["title"] level_value["'beginner'"] --> level["level"] self --> bind title --> bind level --> bind bind --> attributes["self.title and self.level<br/>are stored"]
Unexpected Keyword Arguments in Classes
This FAQ is designed to work as a standalone article. The Open in Colab button above opens an optional notebook where you can run the examples one at a time, inspect the errors, and try each repair.
A class call can look perfectly readable and still fail because one supplied keyword does not match the active method signature. The error is small, but it reveals how classes, methods, self, parameters, and arguments fit together.
Short answer
got an unexpected keyword argument 'level' means the call supplied a named argument, level, but the function or method Python called has no parameter with that exact name. Compare the call with the active definition. Correct a typo, remove data that does not belong, or declare and store a real parameter.
For ordinary instance methods, also verify that self is the first parameter. A missing self changes how every visible argument is matched and may produce this error or a related message such as got multiple values for argument.
What does the error actually mean?
The error is a TypeError because Python found a mismatch between a callable’s definition and the arguments used to call it.
Focus on the final part:
This says:
- The call used the keyword
level. - Python inspected the callable’s accepted parameters.
- No parameter with that exact name could receive the value.
“Unexpected” describes the function signature, not the programmer. Python is reporting that the call and definition do not currently agree.
Start with the named callable
The full message often identifies where Python tried to send the argument:
The dot indicates that __init__ belongs to Workshop. Search for that class and method definition before searching the entire project.
Copy level directly from the message. Look for it in both the call and the definition. This avoids overlooking a small difference such as level, levels, or Level.
How do classes and instances reach __init__?
A class defines a kind of object. An instance is one object created from that class.
Workshop is the class. python_basics and data_lab refer to two separate instances. Each instance stores its own title and level attributes.
When Python evaluates this call:
it performs these beginner-relevant steps:
- Create a new
Workshopinstance. - Call
Workshop.__init__to initialize that instance. - Pass the new instance automatically as the first argument.
- Match the caller’s remaining arguments to
titleandlevel. - Return the initialized instance from the class call.
__init__ is commonly called a constructor in everyday Python teaching. More precisely, it initializes an instance after Python creates it. That distinction becomes useful later, but it does not change this debugging routine.
The class call supplies two visible arguments. Python adds the new instance for self before it calls __init__.
Attributes belong to an instance
These assignments use two different meanings of title and level:
titleon the right is the parameter value received by this call.self.titleon the left is an attribute stored on this instance.levelon the right is another parameter value.self.levelon the left keeps that value after__init__finishes.
Without the attribute assignment, the parameter exists only while the method is running.
What is self, and why must it come first?
self is the conventional name for the instance currently receiving a method call.
In this call:
Python binds:
countertoselfautomatically; and- the visible argument
2toamount.
After the method finishes, counter.value is 2.
self is not a reserved Python keyword. A different parameter name can work, but using self is a strong convention that makes Python code recognizable to other developers. Put it first in ordinary instance methods.
Do all functions inside a class receive self?
Ordinary instance methods do. Python also provides @classmethod, whose first parameter is conventionally named cls, and @staticmethod, which receives no automatic instance or class argument.
Do not omit self accidentally to make an instance method static. If a utility truly belongs with a class but does not use an instance, mark that choice explicitly:
For beginner class designs, start with ordinary instance methods and self. Use a plain function when the behavior does not need object state and does not naturally belong to a class.
What happens when self is missing?
Consider this broken initializer:
A common Python result is:
Why multiple values instead of unexpected keyword?
- Python automatically sends the new instance as the first positional argument.
- Because the method forgot
self, the first declared parameter istitle. - The automatic instance is therefore matched to
title. - The call also explicitly supplies
title="Python". - Python now has two values competing for the same parameter.
The repair is:
Missing self belongs to the same family of argument-binding problems, but the exact error depends on the parameter names and how the method is called. You may see:
got multiple values for argument ...;takes ... positional arguments but ... were given; orgot an unexpected keyword argument ....
Adding, removing, or renaming a keyword until the message disappears can leave the class with the wrong design. First make sure every instance method has self, then compare the remaining parameters with the caller-supplied arguments.
Quick check: self and method binding
How do positional and keyword arguments match parameters?
A parameter is a name in a function or method definition. An argument is a value supplied in a call.
This definition has two parameters:
titleis required because it has no default value.levelis optional because its default is"beginner".
The same matching rules apply to methods after Python handles the automatic instance argument.
Positional arguments use order
Python matches the first value to title and the second to level.
Keyword arguments use names
Python matches each value to the parameter with that exact name. Keyword arguments make many calls easier to read and allow the supplied keywords to appear in a different order:
Positional and keyword arguments can be mixed carefully
Positional arguments must appear before keyword arguments in a call. A parameter must not receive two values:
This raises:
Compare the common messages
| Message fragment | Matching problem | First check |
|---|---|---|
unexpected keyword argument 'level' |
No accepted parameter is named level. |
Compare spelling and the definition. |
missing 1 required positional argument: 'title' |
No value reached required parameter title. |
Supply it or decide whether it needs a default. |
got multiple values for argument 'title' |
Two arguments both reached title. |
Check mixed arguments and missing self. |
takes 2 positional arguments but 3 were given |
More positional values arrived than parameters can receive. | Remember the automatic instance for methods. |
Which fix matches the program?
There is no single repair. Choose the one that matches the program’s meaning.
Fix A: Correct a misspelled keyword
If the parameter is named title, change titel to title. Spelling, capitalization, and underscores must match exactly.
Fix B: Remove an argument that does not belong
If the class should store only a title, the call is wrong:
Do not expand the class merely because one caller supplied unrelated data.
Fix C: Declare and store required data
If every workshop has a level, add both the parameter and attribute:
Adding only self.level = level without declaring level does not work because the method would have no local value named level.
Fix D: Provide a sensible default for optional data
The default lets a caller omit level, while an explicit keyword can override it.
Fix E: Add self to an instance method
After self receives the instance, prefix can correctly receive the visible argument:
Do not use **kwargs as a universal patch
This version accepts any keyword:
It also silently accepts a typo such as levle="beginner" unless the method validates kwargs. That can move the bug farther from its cause. Use **kwargs when the design genuinely needs to collect or forward flexible named arguments, not merely to hide a useful error.
How can I inspect what a class accepts?
The most reliable comparison uses the running code, not memory.
Read the definition
Find class Workshop, then inspect def __init__(...) and the method named in the traceback. Write the declared parameter names beside the supplied keyword names.
Ask Python for the signature
The standard-library inspect module can display accepted parameters:
from inspect import signature
class Workshop:
def __init__(self, title, level="beginner"):
self.title = title
self.level = level
def describe(self, prefix="Workshop"):
return f"{prefix}: {self.title}"
workshop = Workshop("Python")
print(signature(Workshop))
print(signature(Workshop.__init__))
print(signature(workshop.describe))Expected output resembles:
The class signature shows what callers normally supply. The unbound Workshop.__init__ signature includes self. The bound workshop.describe signature omits the already supplied instance.
help(Workshop) and editor parameter hints can also reveal a signature. For a third-party class, compare documentation with the version installed in the current environment.
Rerun a changed class in notebooks
Editing a class cell does not replace the class in a running Colab or Jupyter runtime until that cell executes again.
If the displayed definition includes level but the running class still rejects it:
- Run the class-definition cell again.
- Recreate the instance.
- Run the failing call again.
- If execution history is unclear, restart and run all cells from the top.
See Running Code and Understanding Output for a longer explanation of notebook state.
A repeatable debugging routine
Use this routine for constructors, methods, ordinary functions, dataclasses, and library APIs.
- Read the last traceback line. Record the callable and rejected keyword.
- Find the call. List every positional and keyword argument supplied.
- Find the active definition. Inspect
__init__or the named method. - Account for the automatic instance. An instance method normally begins with
self, which Python supplies. - Match values to parameters. Use order for positional arguments and exact names for keyword arguments.
- Choose a meaning-based fix. Correct a typo, change the call, declare real data, add a default, or restore
self. - Rerun the definition if needed. Notebook state may still contain an older class.
- Change one thing and test again. A different error is new evidence, not a reason to change several unrelated lines.
Copy only the class definition and one failing call into a fresh cell. Replace large data with short strings. If the small version works, restore one removed piece at a time until the mismatch returns.
For a complete method of preparing code to share, use Minimal Reproducible Examples.
Try three diagnoses in Colab
For each case, predict the message family, name the mismatch, and make the smallest meaningful repair.
Case A: a spelling mismatch
Case B: a missing instance parameter
Case C: an optional value
Show one possible repair for each case
Case A: The class accepts name, not student_name. Use the accepted name:
Case B: The method needs self before the caller-supplied name parameter:
Case C: If every student has an active status and True is a useful default, declare and store it:
If active status does not belong in this class, remove it from the call instead.
Check the diagnosis
A compact checklist
unexpected keyword argumentmeans a supplied keyword name is not accepted by the active callable signature.- Calling a class normally initializes a new instance through
__init__. - A method is a function defined in a class, and an instance method receives the current instance through its first parameter, conventionally
self. - Positional arguments match by order; keyword arguments match by exact name.
- Missing
selfmay producemultiple values, a positional-argument count error, or an unexpected-keyword error depending on the call. - Fix the meaning of the mismatch instead of merely silencing the message.
- Inspect the active signature and rerun class definitions in notebooks before making broad changes.
Continue with Object-Oriented Basics to practice classes, instances, attributes, methods, and self in a broader lesson. Use Error Messages to study traceback reading in more detail.