Objects, Attributes, and Methods
Unit ID: M05-U05 Estimated active time: 20-30 minutes
Read dot notation from left to right
course_name = " python foundations "
clean_name = course_name.strip().title()
course_name refers to a string object. .strip and .title are methods available on strings. Parentheses call them.
An attribute is a named value available on an object. A method is callable behaviour reached through the object.
Inspect rather than memorise
print(course_name.__class__)
print(course_name.upper)
print(course_name.upper())
The second line displays a bound method object. The third calls it and returns uppercase text.
Use dir(course_name) to inspect names available on an object, but expect a long result. Use help(str.strip) or help(course_name.strip) for focused documentation.
Library-style code
Later you will read code such as:
table.head()
array.shape
path.exists()
You do not need to know the internal class implementation to read the pattern:
tableobject,head()method;arrayobject,shapeattribute;pathobject,exists()method.
Avoid calling attributes blindly
An attribute value is not always callable. array.shape() would be wrong if shape is a tuple attribute. Documentation tells you whether parentheses are required.
Classes are deferred
Classes define new object types and their behaviour. This course only needs enough object language to read common library code. Designing class hierarchies belongs in a later programming course.
Practice
For a string, inspect upper, upper(), and __class__. Explain which is a method object, which is a returned value, and which describes the object's class.
Takeaway
Dot notation accesses attributes and methods on objects. Parentheses call a method. Read documentation instead of guessing whether a name is data or behaviour. Next, we will import reusable code from standard-library modules.
