FAQ: Learn Python: Classes - Everything is an Object

This community-built FAQ covers the “Everything is an Object” 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 Everything is an Object

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!

6 posts were split to a new topic: Why is there already an attribute?

Hi folks! Appreciate your advice if you can help me with this:

Why do I get different outputs for:

print(dir(this_function_is_an_object))

print(dir(this_function_is_an_object()))

with this_function_is_an_object defined as:

def this_function_is_an_object():
  pass

and what are their differences? Thanks!

In one instance you’re calling dir on the function itself and in the second you’re calling it on the return from that function. Remember that you call a function with that format my_function(args). Try looking at the type of these objects if you’re curious.

For that specific function the return would be the None object since it has no valid return.

2 Likes

Now I see it. Thank you for your explanation!

1 Like

Hi all,

Another wonderful day of not having a clue what is going on in the Codecademy lessons:
Here are instructions for part 12:

1.
Call dir() on the number 5. Print out the results.
2.
Define a function called this_function_is_an_object. It can take any parameters and return anything you’d like.
3.
Print out the result of calling dir() on this_function_is_an_object.

Functions are objects too!

Here is the solution that does not make any sense for part 12:

print(dir(5))

def this_function_is_an_object(num):
  return "Cheese is {} times better than everything else".format(num)

print(dir(this_function_is_an_object))

Does anyone have any thoughts that can explain “Cheese is {} times better than everything else”.format(num) as an answer to “Print out the result of calling dir() on this_function_is_an_object.”?

Thanks again.

I think the point is that the return statement here doesn’t matter. You don’t actually call the function, with the notable difference being the absence of () parentheses following the function name.

The following lines make an example of this difference-

dir(myfunc)  # call dir on the function object itself
dir(myfunc())  # call dir on the return from the function object
2 Likes

Hi there, just for clarification: dir() returns attributes = variables and methods?
thanks, Joachim

print(dir(5))

gives you
[‘abs’, ‘add’, ‘and’, ‘bool’, ‘ceil’, ‘class’, ‘delattr’, ‘dir’, ‘divmod’, ‘doc’, ‘eq’, ‘float’, ‘floor’, ‘floordiv’, ‘format’, ‘ge’, ‘getattribute’, ‘getnewargs’, ‘gt’, ‘hash’, ‘index’, ‘init’, ‘init_subclass’, ‘int’, ‘invert’, ‘le’, ‘lshift’, ‘lt’, ‘mod’, ‘mul’, ‘ne’, ‘neg’, ‘new’, ‘or’, ‘pos’, ‘pow’, ‘radd’, ‘rand’, ‘rdivmod’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘rfloordiv’, ‘rlshift’, ‘rmod’, ‘rmul’, ‘ror’, ‘round’, ‘rpow’, ‘rrshift’, ‘rshift’, ‘rsub’, ‘rtruediv’, ‘rxor’, ‘setattr’, ‘sizeof’, ‘str’, ‘sub’, ‘subclasshook’, ‘truediv’, ‘trunc’, ‘xor’, ‘bit_length’, ‘conjugate’, ‘denominator’, ‘from_bytes’, ‘imag’, ‘numerator’, ‘real’, ‘to_bytes’]
what are these? “eq”, “ge”, “gt”, “le”, “lt”, “ne”, “ror”

3. Data model — Python 3.12.0 documentation

Eg.

>>> a = 5
>>> a.__eq__(5)
True
>>> a.__ne__(4)
True
>>> a.__gt__(4)
True
>>> a.__lt__(6)
True
>>> a.__ge__(5)
True
>>> a.__le__(5)
True
>>> 

Read the documentation for these “rich comparisons” and consider how Python might be using them (dunder methods) in the background to compile our code that uses the comparison operators.

a == 5
a != 4
a > 4
a < 6
a >= 5
a <= 5

When you have reviewed the above linked section, scroll back to the very top and give the full page a quick scan, or partial read to familiarize yourself with the documentation for Data Models, as described in the narrative for this/these current lessons. Bookmark it and refer back to the docs when a question arises, or just to review.

Oh, and the last item in your list is a binary OR method.

3. Data model — Python 3.12.0 documentation

This will not come up anytime soon, but will surface in the more advanced units.

>>> a = 15
>>> b = 31
>>> a & b
15
>>> a | b
31
>>> a | b & a
15
>>> (a | b) & a
15
>>> b = 15
>>> a & b
15
>>> a | b
15
>>> (a & b).__eq__(a | b)
True
>>> 

We may conclude that if A & B is equal to A | B, then A equals B. Save this rabbit hole for January when the snow has you socked in and you have time to really explore binary arithmetic and logic. It is not a light subject, for all its simplicity, which is deceptive. We can miss key information that may be desperately needed at some point later on. If you get into this subject, spend the time to really acquaint yourself. It can not be treated lightly but will have massive influence on your future thinking, logically speaking. Like I said, it is a giant rabbit hole. Be prepared to do the work, else don’t venture down it.

oh! i was being stupid and not thinking enough, they’re just abbreviations of those symbols.
i would have never guessed ‘ror’ as binary or
for your last paragraph, i already know a lot about binary, i know that you can actually count to 255 using just 8 of your fingers. and i also know how a lot of the binary arithmetic, like the hardware behind multiplication when it was still hardware. i obsess over this rabbit hole, i like making computers in circuit simulators like Logisim, and making computers in games not originally intended for that purpose just for the challenge. one such project is a 2 byte 4 core computer work in progress in scrap mechanic, i can talk your ear off about this stuff, i probably already have, but to hammer in that point, i most certainly know binary.

1 Like

thanks for the like, i was trying to make it amusing by the end of that, and apparently i did manage to amuse you.

Why, for example, is the attribute of list __len__, but also pop. Why is one of them dunder method and the other is not?

What’s the really difference between type() and dir() ?