Back to Freecodecamp

Step 3

curriculum/challenges/english/blocks/learn-special-methods-by-building-a-vector-space/65f055b9190fc41ca35549b8.md

latest1.1 KB
Original Source

--description--

Python offers various methods that include both a leading and trailing double underscore in their names. You may already be familiar with some, such as __init__ and __str__. These methods, which follow the __<name>__ naming pattern, are referred to as special methods, magic methods, or dunder (which stands for double underscore) methods.

Defining special methods in a class affects the behavior of that class. They are called under the hood in specific situations (e.g. __init__ during instantiation, __str__ when the object is printed or passed to str()). In this project, you are going to learn some of the most commonly used special methods.

For now, assign x to the x attribute of the Vector object.

--hints--

You should assign x to self.x.

js
({
    test: () => assert(runPython(`
      _Node(_code).find_class("Vector").find_function("__init__").has_stmt("self.x = x")
    `))
})

--seed--

--seed-contents--

py
--fcc-editable-region--
class Vector:
    def __init__(self, x, y):
        pass
--fcc-editable-region--