@@ -9,12 +9,12 @@ simply try the following:
99
1010.. code-block :: pycon
1111
12- >>> type(3)
13- <class 'int'>
14- >>> type("Hello")
15- <class 'str'>
16- >>> type(["Hello", "Pythonistas"])
17- <class 'list'>
12+ >>> type(3)
13+ <class 'int'>
14+ >>> type("Hello")
15+ <class 'str'>
16+ >>> type(["Hello", "Pythonistas"])
17+ <class 'list'>
1818
1919 In these examples you can see the built-in :class: `type ` function in Python. It
2020can be applied to any Python object and returns the type of the object. In this
@@ -29,10 +29,10 @@ you can compare these Python objects with each other:
2929
3030.. code-block :: pycon
3131
32- >>> type("Hello") == type("Pythonistas!")
33- True
34- >>> type("Hello") == type("Pythonistas!") == type(["Hello", "Pythonistas"])
35- False
32+ >>> type("Hello") == type("Pythonistas!")
33+ True
34+ >>> type("Hello") == type("Pythonistas!") == type(["Hello", "Pythonistas"])
35+ False
3636
3737 With this technique you can, among other things, perform a type check in your
3838function and method definitions. However, the most common question about the
@@ -41,47 +41,47 @@ example with a simple inheritance hierarchy makes this clearer:
4141
4242#. First, we define two classes with an inheritance hierarchy:
4343
44- .. code-block :: pycon
44+ .. code-block :: pycon
4545
46- >>> class Form:
47- ... pass
48- ...
49- >>> class Square(Form):
50- ... pass
51- ...
52- >>> class Circle(Form):
53- ... pass
54- ...
46+ >>> class Form:
47+ ... pass
48+ ...
49+ >>> class Square(Form):
50+ ... pass
51+ ...
52+ >>> class Circle(Form):
53+ ... pass
54+ ...
5555
5656 #. Now you can create an instance ``c1 `` of the class ``Circle ``:
5757
58- .. code-block :: pycon
58+ .. code-block :: pycon
5959
60- >>> c1 = Circle()
60+ >>> c1 = Circle()
6161
6262 #. As expected, the ``type `` function on ``c1 `` outputs that ``c1 `` is an
6363 instance of the class ``Circle `` defined in your current ``__main__ ``
6464 namespace:
6565
66- .. code-block :: pycon
66+ .. code-block :: pycon
6767
68- >>> type(c1)
69- <class '__main__.Circle'>
68+ >>> type(c1)
69+ <class '__main__.Circle'>
7070
7171 #. You can also get exactly the same information by accessing the ``__class__ ``
7272 attribute of the instance:
7373
74- .. code-block :: pycon
74+ .. code-block :: pycon
7575
76- >>> c1.__class__
77- <class '__main__.Circle'>
76+ >>> c1.__class__
77+ <class '__main__.Circle'>
7878
7979 #. You can also explicitly check whether the two class objects are identical:
8080
81- .. code-block :: pycon
81+ .. code-block :: pycon
8282
83- >>> c1.__class__ == Circle
84- True
83+ >>> c1.__class__ == Circle
84+ True
8585
8686 #. However, two built-in functions provide a more user-friendly way of obtaining
8787 most of the information normally required:
@@ -92,24 +92,24 @@ example with a simple inheritance hierarchy makes this clearer:
9292 :func: `python3:issubclass `
9393 determines whether one class is the subclass of another.
9494
95- .. code-block :: pycon
96-
97- >>> issubclass(Circle, Form)
98- True
99- >>> issubclass(Square, Form)
100- True
101- >>> isinstance(c1, Form)
102- True
103- >>> isinstance(c1, Square)
104- False
105- >>> isinstance(c1, Circle)
106- True
107- >>> issubclass(c1.__class__, Form)
108- True
109- >>> issubclass(c1.__class__, Square)
110- False
111- >>> issubclass(c1.__class__, Circle)
112- True
95+ .. code-block :: pycon
96+
97+ >>> issubclass(Circle, Form)
98+ True
99+ >>> issubclass(Square, Form)
100+ True
101+ >>> isinstance(c1, Form)
102+ True
103+ >>> isinstance(c1, Square)
104+ False
105+ >>> isinstance(c1, Circle)
106+ True
107+ >>> issubclass(c1.__class__, Form)
108+ True
109+ >>> issubclass(c1.__class__, Square)
110+ False
111+ >>> issubclass(c1.__class__, Circle)
112+ True
113113
114114 .. _duck-typing :
115115
@@ -145,16 +145,16 @@ instances in a readable format:
145145
146146.. code-block :: pycon
147147
148- >>> class Form:
149- ... def __init__(self, x, y):
150- ... self.x = x
151- ... self.y = y
152- ... def __str__(self):
153- ... return "Position: x={0}, y={1}".format(self.x, self.y)
154- ...
155- >>> f = Form(2, 3)
156- >>> print(f)
157- Position: x=2, y=3
148+ >>> class Form:
149+ ... def __init__(self, x, y):
150+ ... self.x = x
151+ ... self.y = y
152+ ... def __str__(self):
153+ ... return "Position: x={0}, y={1}".format(self.x, self.y)
154+ ...
155+ >>> f = Form(2, 3)
156+ >>> print(f)
157+ Position: x=2, y=3
158158
159159 Even though our special ``__str__ `` method attribute was not explicitly called
160160by our code, it could still be used by Python because Python knows that the
0 commit comments