Skip to content

Commit

Permalink
Merge branch 'master' of github.com:IntermediatePython/intermediatePy…
Browse files Browse the repository at this point in the history
…thon.github.io
  • Loading branch information
yasoob committed Aug 17, 2015
2 parents 3ea16bb + 4748338 commit ae150eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
14 changes: 7 additions & 7 deletions __slots__magic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ In Python every class can have instance attributes. By default Python
uses a dict to store an object’s instance attributes. This is really
helpful as it allows setting arbitrary new attributes at runtime.

However, in small classes with known attributes it might be a
However, for small classes with known attributes it might be a
bottleneck. The ``dict`` wastes a lot of RAM. Python can’t just allocate
a static amount of memory at object creation to store all the
attributes. Therefore it sucks a lot of RAM if you create a lot of
classes (I am talking in thousands and millions). Still there is a way
objects (I am talking in thousands and millions). Still there is a way
to circumvent this issue. It involves the useage of ``__slots__`` to
tell Python not to use a dict, and only allocate space for a fixed set
of attributes. Here is an example with and without ``__slots__``:
Expand All @@ -19,9 +19,9 @@ of attributes. Here is an example with and without ``__slots__``:
.. code:: python
class MyClass(object):
def __init__(name, class):
def __init__(name, identifier):
self.name = name
self.class = class
self.identifier = identifier
self.set_up()
# ...
Expand All @@ -30,10 +30,10 @@ of attributes. Here is an example with and without ``__slots__``:
.. code:: python
class MyClass(object):
__slots__ = ['name', 'class']
def __init__(name, class):
__slots__ = ['name', 'identifier']
def __init__(name, identifier):
self.name = name
self.class = class
self.identifier = identifier
self.set_up()
# ...
Expand Down
9 changes: 5 additions & 4 deletions targeting_python_2_3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ in Python 2 you can import it from ``__future__``.
# Output:
from __future__ import print_function
print
print(print)
# Output: <built-in function print>
**Using ``as`` in imports**
**Dealing with module renaming**

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


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

0 comments on commit ae150eb

Please sign in to comment.