BeginnerCoder

Lesson 7 — Catch a made-up function

When the method does not exist

Sometimes an assistant calls a function or method that does not exist — it sounds plausible, so it gets written. This is often called a hallucination. A common one: .trim(), which exists for strings in JavaScript, but in Python the method is .strip().

You will see an error like:

AttributeError: 'str' object has no attribute 'trim'

How to check what really exists:

  • hasattr(str, "strip") — True or False;
  • dir(str) — lists every method of strings;
  • the official Python docs at docs.python.org.

Do not fix it by asking the assistant to "try again" and hoping. Find the real method, then fix it yourself (or tell the assistant exactly which method to use).

Why it matters: a made-up function can hide in a rarely used branch of the code and only crash months later, in front of a user.

Want it another way?
Task

The AI used a string method that does not exist in Python. Use the hasattr checks to find the real one and fix normalize_username().

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.