Course 1.1Lesson 1 of 5
Every value in Python is an object: it has data, called attributes, and actions, called methods, and you reach both with a dot. A string, a list and even a number are objects of a kind, called a class, and type tells the class of a value. A method is called with brackets and an attribute is read without them. dir lists what an object can do. A class is a kind of thing, and an object is one example of it. Knowing this makes the idea of writing your own class natural.
Try the code and the simulations beside the lesson.
After this lesson you can
word.upper() and in z.real,dir.You have written word.upper() and numbers.sort() many times. The dot says: "the action upper of the thing word". That thing, word, is an object. An object keeps some data, and knows some actions that work on that data. The data are called attributes. The actions are called methods.
Predict first, then run it.
type("python") is <class 'str'>. The word class is the new idea. A class is a kind of thing: all strings are of the class str, all lists of the class list. An object is one example of a kind. "python" is one object of the class str.word.upper() is a method: an action that a string knows how to do. It is called with brackets.numbers.sort() is a method of a list. It changes the list, and gives back None.(3 + 4j).real is an attribute: a piece of data that belongs to the object. A complex number keeps a real part and an imaginary part. We read it without brackets.dir(word) lists the names that a string knows. A string has about 45 public methods, and you have met only a few.word.upper == "PYTHON" is False, because word.upper without brackets is the method itself, not its answer. The brackets call it.| Word | Meaning | Example |
|---|---|---|
| class | a kind of thing | str, list, int |
| object (instance) | one example of a class | "python", [3, 1, 2] |
| attribute | data that belongs to an object | z.real |
| method | an action that belongs to an object | word.upper() |
| the dot | reach into an object | word.upper |
An object is a package of data and actions. A class is the description of the package: what data it has, and what it can do. You write the description once, and you can make as many objects from it as you like.
Attributes are read without brackets. A method is called with them. The object carries both, so z knows its own parts.
You can do a lot with functions and dictionaries, and you have. So why classes?
Think about a student with a name and some marks. With a dictionary, you write {"name": "Asha", "marks": [72, 85]}, and a function average(student). The data is in one place and the function is in another. Nothing stops someone from putting a text where the marks should be, or from calling average with a dictionary that has no marks.
With a class, the data and the actions live together. A Student object knows its own marks, and student.average() works on those marks. The class can also protect its data, with rules: "a mark must be from 0 to 100". As programs grow, this keeps things tidy. Large programs are built from objects that each look after themselves.
word.upper is the method, and word.upper() is its answer.z.real() is a TypeError, because a number is not something to call.str is a class. "python" is an object of it.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
type and dir and the checker confirms the answers.type(x) gives the class of x, and dir(x) lists what it can do.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
"python"?word.upper and word.upper()?z.real or numbers.sort()?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.