Guys what's this saying?

what’s this page saying?
Python: Object-Oriented Programming | Codecademy

That is a pretty broad question. Can you tell us where it is that you hit a wall in understanding the narrative?


oh, i just realized the link takes you to the whole lesson, not the page…
it’s page 11


Since we can already use dictionaries to store key-value pairs, using objects for that purpose is not really useful. Instance variables are more powerful when you can guarantee a rigidity to the data the object is holding.

This convenience is most apparent when the constructor creates the instance variables using the arguments passed into it. If we were creating a search engine and wanted to create a class to hold each search entry, we could do so like this:

class SearchEngineEntry:  def __init__(self, url):    self.url = urlcodecademy = SearchEngineEntry("")wikipedia = SearchEngineEntry("")print(codecademy.url)# prints ""print(wikipedia.url)# prints ""

In the preceding code sample, we define a SearchEngineEntry class, which contains a constructor with two parameters, self and url. Inside the constructor body, we create an instance variable named self.url and assign it the value of the url parameter that is passed into the constructor.

We then create the codecademy instance of SearchEngineEntry by passing the URL as an argument to the constructor. After codecademy is defined, printing codecademy.url shows that the URL has been assigned to the url instance variable of codecademy. Similarly, wikipedia.url holds the value that was passed into the constructor when wikipedia was defined.

Since the self keyword refers to the object and not the class being called, we can define a .secure() method on the SearchEngineEntry class that returns the secure link to an entry.

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("")wikipedia = SearchEngineEntry("")print( prints ""print( prints ""

Above, we define our .secure() method to take just the one required argument, self. We access both the class variable self.secure_prefix and the instance variable self.url to return a secure URL.

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.

could someone say what’s this is saying?

You’ve still left us with a broad question. We read that same narrative, yesterday. Where are getting lost in it?

The entire page :! Literally!

Hi! i guess i finally realized what i don’t get about it. i guess i don’t get difference of item and instance variable

When referring to Python dictionaries, an item is a dictionary member, as in, a key-value pair.

>>> a = {
        'a': 'one',
        'b': 'two',
        'c': 'three'
>>> print (a.items())
    dict_items([('a', 'one'), ('b', 'two'), ('c', 'three')])

Instance variables are the built in variables of a class instance.

>>> class Foo:
            def __init__(self, obj):
                self.obj = obj

>>> bar = Foo(a)    #  a is defined above
>>> print (bar.obj.items())
    dict_items([('a', 'one'), ('b', 'two'), ('c', 'three')])

Above, bar is an instance of the Foo class. bar.obj is the instance variable, namely an attribute of the instance.

>>> dir(bar)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'obj']

Scroll to the vary end of the list; see the instance attribute, ‘obj’?

>>> hasattr(bar, 'obj')

Be sure you continue reading about these concepts over the weekend before you move on from the lesson. It is fundamental to our understanding and cannot be disregarded or considered unimportant. Build your foundation slowly and steadily, and go back to review, frequently.

1 Like