Related article:
Transcript:
Classes are everywhere in Python. Even if you never define your own type of class, you will certainly use classes in Python.
Using a class object
We've imported class Point here, that we're calling. When you call a class, you get back an instance of that class:
>>> from point import Point
>>> p = Point(1, 2, 3)
We can access the x attribute on this Point object (aka Point instance) or call the is_origin method on it:
>>> p.x
1
>>> p.is_origin()
False
Python data structures are classes
When we check the type of p (which is a Point instance) it will tell us I am an object of type "class Point":
>>> type(p)
<class 'point.Point'>
The type of something is the same as the class of that thing. So class instances (aka objects of some class/type) can have attributes and they can have methods.
We've seen attributes and methods in other places in Python too. For example, lists have an append method:
>>> numbers = [1, 2, 3]
>>> numbers.append(4)
>>> numbers
[1, 2, 3, 4]
If you ask lists for their type, you'll see some kind of class. You always get a class when you ask for the type of something in Python:
>>> type(numbers)
<class 'list'>
The type of a list is the list class. So list is not a function in Python, it's a class. When you call list with parentheses after it, you get an empty list back because we're constructing a new object of type list:
>>> list()
[]
All the built-in data structures in Python are classes:
>>> tuple
<class 'tuple'>
>>> dict
<class 'dict'>
>>> set
<class 'set'>
Everything that has a "type" has a "class"
Interestingly, some of the even more fundamental types of objects are actually classes.
Anything that has a type has a class.
When you ask for the type of a string you'll see str:
>>> name = "Trey"
>>> type(name)
<class 'str'>
We may have thought of str as a function that can convert something to a string:
>>> str(4)
'4'
But it's actually a class!
int is also a class:
>>> x = 4
>>> type(x)
<class 'int'>
in fact, there are even attributes we can access on instances of the int class:
>>> x.real
4
>>> x
4
We're accessing the real attribute of object to get the real component of this number, which is the same as the number itself.
So, Integers are a class, which means if we call the int function, we're actually calling a class to get back an integer instance:
>>> int('4')
4
Built-in functions are class
A lot of the built-in functions in Python are actually classes too.
For example, when we call the enumerate function, we'll get back an enumerate object:
>>> enumerate(numbers)
<enumerate object at 0x7f58e8228c00>
The enumerate "function" is actually a class:
>>> enumerate
<class 'enumerate'>
It's not incorrect to say "the enumerate function" even though enumerate is implemented as a class because the distinction between classes and functions isn't very important in Python.
Summary
The distinction between classes and functions doesn't really matter that much in Python. The thing that I care about is the behavior: I care that I can call it.
When you call a function, we'll get the return value of that function. When you call a class, we'll get an instance of that class. Both functions and classes are callables and that's the thing we usually care about ("can I call it?").
So classes are everywhere in Python! Even if you never make your own class, you will certainly use classes.
from Planet Python
via read more
No comments:
Post a Comment