We know that a function looks like this:
And we know that a function in a package looks like this:
So is list
a package?
my_list.append( <value> )
is a function.
my_list.append( <value> )
is a special type of function called a method.
Packages group useful constants and functions together in one place.
Methods group constants and functions together in one place with data.
So my_list.append(...)
is called a list method:
[]
or [1,'dog',3.5]
).This will give you:
Help on list object:
class list(object)
| list(iterable=(), /)
| Built-in mutable sequence.
| If no argument is given, the constructor creates a new empty list.
| Methods defined here:
| ...
| append(self, object, /)
| Append object to the end of the list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
| ...
msg = 'Hello World'
dir(msg)
['__add__', '__class__', ..., 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', ... 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
And then we can inquire about these methods:
my_list = []
can also be written my_list = list()
, and my_string = 'Foo'
can be written my_string = str('Foo')
.
From here on out, nearly all of what you learn will be new applications, not new concepts and terminology.
Term | Means | Example |
---|---|---|
Class | The template for a ‘thing’. | Recipe for a pizza. |
Object | The instantiated ‘thing’. | A pizza I can eat! |
Method | Functions defined for the class and available to the object. | Things I can do with a pizza (eat, cook, make). |
Constructor | The special method that builds new objects of that class. | How to start a new pizza! |
Self | A reference to the current object. | The pizza in front of me! |
Methods • Jon Reades