FAQ: Learn Python: Classes - Self

This community-built FAQ covers the “Self” exercise from the lesson “Learn Python: Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Self

Community Tips and Resources

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: Where is my typo?

13 posts were split to a new topic: What are the differences between instance variables and class variables, or why do I need to use self.pi?

2 posts were merged into an existing topic: What are the differences between instance variables and class variables, or why do I need to use self.pi?

2 posts were split to a new topic: Why does circumference require ()?

A post was split to a new topic: Python: Understanding Classes and Self

3 posts were split to a new topic: When should I create a new method vs placing it in init?

3 posts were merged into an existing topic: What are the differences between instance variables and class variables, or why do I need to use self.pi?

Can anyone explain what do they mean by this explanation?

This is the strength of writing object-oriented programs. We can write our classes to structure the data that we need and write methods that will interact with that data in a meaningful way.

What is the difference between that and what we have been doing so far? Only difference I see is that we put functions inside class, so like, we still have the same tools but now they are inside boxes meant to store them. But when they say “This is the strength of OOP” what is “this”?

Now we can group methods which are doing similar operations.

data types are also classes, for example .append() and .remove() belong to list class.

so when:

the_list = [1, 2, 3]

an instance of list class is created. And now we can do operations on the list

But what is the actual advantage? Wether I define functions or variables inside or outside of a class I can still do operations on them so is this whole object oriented programming adding any usefull functionality or is just a way to make your code more readable by segregating functions into separate containers?

readability and maintainability is an advantage. OOP isn’t some holy grail. Use it when you need to.

This is the problem with OOP, at first it doesn’t seem to add much value, until you get to a larger project/codebase and you start seeing how inheritance and instance and class variable can structure your code nicely.

you can’t mutate global variable in a function (okay, you could with global, but you shouldn’t). So being able to have the data in a class (as instance variable for example) and being able to update the instance variable is an advantage

besides, updating global variable can lead to all kind of race conditions.


I found myself a bit confused and overwhelmed by all these new methods and syntaxes within this course. It’s a bit too much to digest. Hopefully I will understand it better with some exercises.

Okay, so I’ve gone through this chapter a second time and I finally understand the examples given in this chapter by breaking it down line by line. I found the second example given in this chapter a tad too convoluted. So I simplified it a little bit and added my notes. Perhaps it could help fellow learners to understand:

# More secure way by adding "https://" in front of the url:
class SearchEngineEntry:

  # Remember the constructor __init__ is called every time the Class is instantiated:   
  def __init__(self, url):
    self.showurl = url

  # A method is added within the Class:
  def secure(self):
    # Here we use string interpolation to concatenate the prefix with the url (in this case an instance variable):
    return "https://{site}".format(site=self.showurl)

# Outside the indented area, the Class is instantiated, therefore __init__ method is immediately called.
codecademy = SearchEngineEntry("www.codecademy.com")

# You can still call the url without the prefix, remember there is no () here because you're calling an instance, the param is already entered when it was instantiated:
# prints "www.codecademy.com"

# To actually call the prefix, you call the class method secure():
# prints "https://www.codecademy.com"

In the original example, the attribute url and parameter url was named the same, which confused me a lot. So I gave the attribute a different name showurl.

Furthermore, the https:// in the example was first defined as a class variable, prior to being called using self.attribute syntax and called within string interpolation. I understand they’re trying to show the usage of both class variable and instance variable here, and it’s probably considered good practice too, but it is just too convoluted for the average learner to understand.

Hope this helps!

1 Like

This is standard practice. It should not be confusing, at all. We assign class attribute that matches the parameter. Why have a different variable name? That was my first question on reading that line.

You’re right, I realized that as I progressed further through that chapter. I no longer need to differentiate it. But it did help me to understand it better at first. :slight_smile:


Is it normal that in these lines of code the wikipedia object is not created but is being called in the print() function?

class SearchEngineEntry:
secure_prefix = “https://”
def init(self, url):
self.url = url

def secure(self):
return “{prefix}{site}”.format(prefix=self.secure_prefix, site=self.url)

codecademy = SearchEngineEntry(“www.codecademy”)


prints “https://www.codecademy


prints “https://www.wikipedia

P.S. I removed the .com and .org because of the new users regulations

This could should throw an error, wikipedia is undefined variable. You should first create a class instance.

The thing is that this is the code that is given as an example in an excercise. That’s why I’m asking about it

This line of code:

wikipedia = SearchEngineEntry("www.wikipedia.org")

from the first example should also be in the second example.