Objects and Attributes

An object is a value. Many objects carry named parts called attributes.

You read an attribute with a dot:

config.learning_rate

The expression says: from the object named config, read the attribute named learning_rate.

Dictionaries versus attributes

A dictionary uses keys:

An object uses attributes:

print(config.learning_rate)

Both styles name data. Dictionaries are flexible. Objects can make a fixed shape more explicit.

Many values in Python are objects

Strings have methods:

Lists have methods:

In both examples, the dot asks the object for something attached to it.

Attribute errors

If an attribute does not exist, Python raises AttributeError:

An attribute that does not exist

Runs locally with Python in your browser.

Ready to run.

The useful question is:

What kind of object is this, and what attributes or methods does it actually have?

Use type(value) when you are unsure.

Exercise: Read the attribute

In this code, what expression reads the number of epochs?

config.epochs

Answer it first, then check.

HintRead the expression already shown

Dot notation places the object before the dot and the attribute after it.

SolutionRead config.epochs

The object is config and the attribute is epochs, so the complete expression is config.epochs.