Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Instant Objects Methods

Does 'self' in Python basically behave the same as 'this' in JavaScript?

Are there any major differences? I am already fairly proficient in JavaScript and it was the first language I learned so I have a bit of a bias toward interpreting Python through my understanding of JS, but I don't want to end up getting them confused.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

In a very general sense, yes, the self in python is used to point to the current instance much in the same manner as the this is used in JavaScript to point to the current object. Additionally, the variable name "self" is merely a convention. The first variable named in class method definitions could be "this" and it would still work, but that (or this) would be confusing to regular Python coders. Consider the following code:

this.py
class SomeClass():
    def __init__(this):
        this.attrib = "confusing to python coders, but works"

    def __str__(this):
        return f"This is {this.attrib}!"


s = SomeClass()
print(s)
$ python this.py
This is confusing to python coders, but works!

Thank you Chris.