Can you rename self in OOP Python

In a class, self is the default variable for the instance of the class. IS there a way to rename it somehow, so instead of self.name it is something.name?

Just change the name of the first parameter on every instance method:

class Example:
    def __init__(something, name):
        something.name = name
    def print_name(me):
        print(me.name)

ex = Example("myself")
ex.print_name() # myself
5 Likes

While this is possible, it is generally not recommended to rename self to something else. See PEP 8 Style Guide.

1 Like

If you’re used to JavaScript, you can rename it to this, it’s always the first parameter of the function.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.