Skip to content

Commit ae150eb

Browse files
committed
Merge branch 'master' of github.com:IntermediatePython/intermediatePython.github.io
2 parents 3ea16bb + 4748338 commit ae150eb

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

__slots__magic.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ In Python every class can have instance attributes. By default Python
55
uses a dict to store an object’s instance attributes. This is really
66
helpful as it allows setting arbitrary new attributes at runtime.
77

8-
However, in small classes with known attributes it might be a
8+
However, for small classes with known attributes it might be a
99
bottleneck. The ``dict`` wastes a lot of RAM. Python can’t just allocate
1010
a static amount of memory at object creation to store all the
1111
attributes. Therefore it sucks a lot of RAM if you create a lot of
12-
classes (I am talking in thousands and millions). Still there is a way
12+
objects (I am talking in thousands and millions). Still there is a way
1313
to circumvent this issue. It involves the useage of ``__slots__`` to
1414
tell Python not to use a dict, and only allocate space for a fixed set
1515
of attributes. Here is an example with and without ``__slots__``:
@@ -19,9 +19,9 @@ of attributes. Here is an example with and without ``__slots__``:
1919
.. code:: python
2020
2121
class MyClass(object):
22-
def __init__(name, class):
22+
def __init__(name, identifier):
2323
self.name = name
24-
self.class = class
24+
self.identifier = identifier
2525
self.set_up()
2626
# ...
2727
@@ -30,10 +30,10 @@ of attributes. Here is an example with and without ``__slots__``:
3030
.. code:: python
3131
3232
class MyClass(object):
33-
__slots__ = ['name', 'class']
34-
def __init__(name, class):
33+
__slots__ = ['name', 'identifier']
34+
def __init__(name, identifier):
3535
self.name = name
36-
self.class = class
36+
self.identifier = identifier
3737
self.set_up()
3838
# ...
3939

targeting_python_2_3.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ in Python 2 you can import it from ``__future__``.
3737
# Output:
3838
3939
from __future__ import print_function
40-
print
40+
print(print)
4141
# Output: <built-in function print>
4242
43-
**Using ``as`` in imports**
43+
**Dealing with module renaming**
4444

4545
First tell me how you import packages in your script ? Most of us do
4646
this :
@@ -64,9 +64,9 @@ code below :
6464
.. code:: python
6565
6666
try:
67-
import urllib.request as urllib_request #for python 3
67+
import urllib.request as urllib_request # for python 3
6868
except ImportError:
69-
import urllib2 as urllib_request # for python 2
69+
import urllib2 as urllib_request # for python 2
7070
7171
So let me explain the above code a little. We are wrapping our importing
7272
code in a try except clause. We are doing it because in python2 there is
@@ -111,6 +111,7 @@ functionality in Python 2. For instance we have:
111111
- singledispatch ``pip install singledispatch``
112112
- pathlib ``pip install pathlib``
113113

114+
114115
I am sure there are a lot of other methods and tricks which can be used
115116
to make you code compatible with both of these Python series. This was
116117
just to give you some ideas.

0 commit comments

Comments
 (0)