Everything is an Object

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/data-types/exercises/everything-is-an-object

This is really interesting that a function has so many attributes. My questions is how do I know which of these attributes are methods? And is there a way to see what the definitions of these attributes are?

Thank you so much in advance! :smile:

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Methods with the double underscore are called dunder methods. Read this for a primer: https://medium.com/python-features/magic-methods-demystified-3c9e93144bf7

Additionally, it doesn’t hurt to experiment at the terminal:

def test(test):
    return test

dir(test)
# [ '__annotations__', '__call__', '__class__', '__closure__',
# '__code__', '__defaults__', '__delattr__', '__dict__', etc....

test.__sizeof__
# <built-in method __sizeof__ of function object at 0x7f8d9c148cb0>

test.__sizeof__()
# 112

Very cool!

Also,

image

In this attributes list, I see some are dunder methods, but what about the others? How do I know which ones are methods and which ones are attributes?

append, copy, count, index, pop, reverse, sort are all common list methods.

In common python types (like list and string) you can also type


test_list = []
test_string = 'hi'
help(test_list)
help(test_string)

and get detailed documentation on that particular type.

1 Like

Beautiful, thank you!