8000 Explain methods more · dunstanms/python-tutorial@decae03 · GitHub
[go: up one dir, main page]

Skip to content

Commit decae03

Browse files

Original file line numberDiff line numberDiff line change
@@ -237,13 +237,14 @@ But is `stackoverflow.info` the same thing as `Website.info`?
237237

238238
It's not.
239239

240-
Instead, `stackoverflow.info` is a **method**. Functions as class
241-
attributes can be accessed as methods from instances. Effbot also
242-
has a `.info` method. So `Website.info(stackoverflow)` does the same
243-
thing as `stackoverflow.info()`, and when `stackoverflow.info()` is
244-
called it automatically gets `stackoverflow` as an argument.
245-
246-
In other words, `Class.method(instance)` does the same thing as
240+
Instead, `stackoverflow.info` is a **method**. If we set a function as a
241+
class attribute, the instances will have a method with the same name.
242+
Methods are "links" to the class attribute functions. So
243+
`Website.info(stackoverflow)` does the same thing as `stackoverflow.info()`,
244+
and when `stackoverflow.info()` is called it automatically gets
245+
`stackoverflow` as an argument.
246+
247+
In other words, `Class.method(instance)` does the same thing as
247248
`instance.method()`. This also works with built-in classes, for
248249
example `'hello'.lower()` is same as `str.lower('hello')`.
249250