liveBook · Manning

Dotted notation

As explained in The Quick Python Book, Fourth Edition

Dot notation is syntax that accesses an attribute or method by placing its name after an object and a period.

Dotted notation accesses an attribute or method by placing its name after an object and a period. Use it to read or assign object fields, call methods, and access functions exposed by imported modules. section 15.1.1 section 3.2.1

Common forms include:

  • `object.attribute` reads a field, while `object.attribute = value` assigns or replaces it. Python instances can receive fields on the fly, without declaring them in advance. section 15.1.1
  • `object.method(arguments)` calls a method attached to the object, such as `x.reverse()` or `x.upper()`. Methods operate on their associated object; string methods return values without modifying immutable strings. section 3.2.2 section 6.4
  • `module.function(arguments)` calls a function imported from a library, as in `math.ceil(3.49)`. section 3.2.1
  • Special methods and inherited methods are also reached through object attributes. A method call supplies the instance as its first `self` parameter implicitly. section 3.6

Fields and methods

class Circle: def __init__(self): self.radius = 1 c = Circle() print(c.radius) # 1 c.radius = 5 print(2 * 3.14 * c.radius) # 31.4 The same syntax distinguishes field access (`c.radius`) from a method call (`c.area()`). section 15.1.1 section 3.6

round(3.49)                           #1
 
3
 
import math
math.ceil(3.49)                       #2
 
4
#1 round called with 3.49
#2 The math library module is imported, and its ceil function is called.

Compare a built-in function call with a dotted call to an imported module function: `round(3.49)` versus `math.ceil(3.49)`. section 3.2.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines attribute and method syntax through examples, then connects it to initialization, inheritance, and immutable strings.
Examples
Demonstrates circle fields, complex-number parts, math functions, list methods, inherited movement, and string capitalization.
What it assumes
Assumes general Python expression, function-call, class, instance, list, string, and import familiarity.
What you can do after
Apply it to read and assign attributes, call methods, and invoke functions through imported modules.
How it connects
Links attributes with objects, classes, methods, modules, inheritance, initialization, lists, strings, and complex numbers.

Also explained in

Compare how 9 books explain dotted notation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.