Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Subclassing Built-ins

What does subclassing built ins mean?

I am really confused about advanced objects set of videos. Can someone please explain the following?

What are built in classes? Are these classes which already are built in so we don't need to define their methods and attributes, these are already defined by Python?

In the examples in this video which are the built in classes? How are they being subclassed?

In the JavaScriptObject(dict) and FilledList(list) why isnt the parent class written with a capital Dict or List? Are Dict and List the parent classes, built in?

Magic Methods are methods which are run automatically but their block of code needs to be specified by us. Those are not built in.

Thanks

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Rather than creating a new thread on this, I thought we might be able to revive this one and see if we can get an answer on this. I'm struggling with this too. :-)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

All great questions!

:point_right: What are built in classes? Are these classes which already are built in so we don't need to define their methods and attributes, these are already defined by Python?

A Python release includes a huge library of modules. See Python Standard Library. Because of the size of the standard library, only the commonly needed modules come as "Built-in". That is, the "built-in" classes or types are the ones accessible without having to import an additional module from the standard library. These can be referenced immediately in code or the REPL.

Classically, you might think that the built-in classes are the tangible objects: int, float, complex, list, tuple, range, str, bytes, bytearray, set, dict. But since "everything in Python is an object" and all objects derive from classes, even functions are from a class: builtin_function_or_method. All of the following are built-ins.

abs()           dict()      help()          min()       setattr()
all()           dir()       hex()           next()      slice()
any()           divmod()    id()            object()    sorted()
ascii()         enumerate() input()         oct()       staticmethod()
bin()           eval()      int()           open()      str()
bool()          exec()      isinstance()    ord()       sum()
bytearray()     filter()    issubclass()    pow()       super()
bytes()         float()     iter()          print()     tuple()
callable()      format()    len()           property()  type()
chr()           frozenset() list()          range()     vars()
classmethod()   getattr()   locals()        repr()      zip()
compile()       globals()   map()           reversed()  __import__()
complex()       hasattr()   max()           round()  
delattr()       hash()      memoryview()    set()
False, True, None, NotImplemented, Ellipsis, __debug__
4.1. Truth Value Testing
4.2. Boolean Operations — and, or, not
4.3. Comparisons
4.4. Numeric Types — int, float, complex
4.5. Iterator Types
4.6. Sequence Types — list, tuple, range
4.7. Text Sequence Type — str
4.8. Binary Sequence Types — bytes, bytearray, memoryview
4.9. Set Types — set, frozenset
4.10. Mapping Types — dict
4.11. Context Manager Types
4.12. Other Built-in Types
4.13. Special Attributes

Any other classes have to be imported from modules provided with the library, such as, from datetime import date, or imported from other modules download and installed by the user from other sources, such as, django, numpy, pandas, etc.

:point_right: In the examples in this video which are the built in classes? How are they being subclassed?

In the video, the three built-in classes str, list, and dict, are subclassed by the defined classes ReversedStr, FilledList, JavaScriptObject, respectively. A class, the parent, is subclassed by listed the class in the parens of the child class's signature line.

# define a class
class NameOfNewSubclass(NameOfParentClass):
    pass

:point_right: In the JavaScriptObject(dict) and FilledList(list) why isnt the parent class written with a capital Dict or List? Are Dict and List the parent classes, built in?

For certain built-in classes, the names were established based from their underlying C code. This was before PEP 8 class names convention was established. Python 2.0 was released in 2000. PEP 8 was created in 2001.

:point_right: Magic Methods are methods which are run automatically but their block of code needs to be specified by us. Those are not built in.

The magic methods, or special methods, only need to be defined by us if we need to override their behavior or create additional behavior that hasn't been defined yet. These magic methods are primarily used to help Python figure out what to do when an object is used in a specific context. If len() is called on a class instance, then the __len__ method is looked for to know how to generate the "length" for this instance. Similarly, the __add__ method is looked for if a class instances is followed by a plus-sign (+). For an int class instance, it would be to add the values. For a str class instance, it would be to return a concatenated string.

Post back if you have any more questions! Good luck!!

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Bookmarking this for bed time reading :)

Thanks for a comprehensive reply :)

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

Holy crap this is great! Thanks!!!