In most object-oriented programming languages, we can refer to the newly created object and its methods with `this`; ‘this’ is a special keyword in many programming languages. It is a keyword that references the current object. This allows us to write class methods that make modifications to an object. Without it, we would not be able to make changes to the object internally. Here’s an example:

//Create Class
class Dog {
//Assign properties to the newly created object in the constructor using "this"
constructor(name, breed, age) { = name;
this.breed = breed;
this.age = age;
bark() {
console.log("Bark bark!");
/* Update properties using set methods; set updates the "this" properties
* of the object.
setName(name) { = name;
setBreed(breed) {
this.breed = breed;
setAge(age) {
this.age = age;
getName() {
getBreed() {
return this.breed;
getAge() {
return this.age;
//Create new Dog object
const myDog = new Dog("Isabelle", "Golden Retriever", 2);
console.log(myDog); //Dog { name: 'Isabelle', breed: 'Golden Retriever', age: 2 }
//Update name property using setName method
console.log(myDog); //Dog { name: 'Molly', breed: 'Golden Retriever', age: 2 }
view raw class-example.js hosted with ❤ by GitHub

As you can see in the Javascript example, we use `this` in the constructor to add properties to our object when we create it using the `new` keyword.  Our methods use the this keyword to update the name property at the name of the code as well. Python shows a similar pattern with only one or two fundamental changes.


In Python, `this` is replaced with a new keyword `self`. They fundamentally do the same thing. The only difference is the spelling of the keyword. Take a look at this example:

#! /usr/local/bin/python3
class Dog:
#Python's constructor; setting properties similar to JS
def __init__(self, name, breed, age): = name
self.breed = breed
self.age = age
#Using self to update the name property on the newly created object
def set_name(self, name): = name
def set_breed(self, breed):
self.breed = breed
def set_age(self, name): = name
def get_name(self):
def get_breed(self):
return self.breed
def get_age(self):
return self.age
def __str__(self):
return ("%s %s %d" % (, self.breed, self.age))
# Create new dog object
myDog = Dog("Isabelle", "Golden Retriever", 2)
print(myDog) #Isabelle Golden Retriever 2
#Update my dog's name to Molly
print(myDog) #Molly Golden Retriever 2
view raw hosted with ❤ by GitHub

As you can see the two examples are not that different from each other. If you look closely, they both have a similar pattern when constructing the class. The only difference here is that in Python we have to use the `self` keyword to do the same thing we would do in Javascript. But, this applies to programming languages as a whole; each programming language follows this pattern if they follow the object-oriented paradigm.

Advice For Any Language

My advice to you is to not just learn a programming language, but also understand the paradigm it belongs to; understanding a paradigm helps you gain clarity on different ways to program. That way you can spot the patterns across languages; programming languages come and go, but the way people program is here to stay. With that, key tool, you can get up and running in a language very quickly.Stay tuned for more posts on Python and programming!

%d bloggers like this: