From b14ed96b097679172d7ca77b69efe9ba092e3af5 Mon Sep 17 00:00:00 2001 From: Chris Barker Date: Sat, 2 Jan 2021 15:05:03 -0800 Subject: [PATCH] Removed usages and examples of subclassing from (object) from py2 --- .../context_managers/context_manager.py | 2 +- .../examples/metaprogramming/my_solution.py | 9 +-- .../examples/metaprogramming/start_class.py | 5 +- .../examples/multiple_inheritance/diamond.py | 2 +- .../multiple_inheritance/diamond_super.py | 2 +- source/examples/multiple_inheritance/mro.py | 2 +- .../multiple_inheritance/super_test.ipynb | 63 ++++++++++--------- .../multiple_inheritance/super_test.py | 2 +- source/examples/nosql/address_book_model.py | 14 ++--- source/examples/nosql/address_book_mongo.py | 7 ++- source/examples/pep8/listing1.py | 14 ++--- source/examples/wikidef/definitions.py | 2 +- source/exercises/html_renderer/html_render.py | 12 +++- .../html_renderer/html_renderer_tutorial.rst | 6 +- source/modules/BasicPython.rst | 6 +- source/modules/ContextManagers.rst | 5 +- source/modules/MetaProgramming.rst | 3 +- source/modules/PythonClasses.rst | 32 +++++----- source/solutions/Extras/persistance/circle.py | 2 +- .../Lesson07/version2/html_render.py | 2 +- source/solutions/Lesson08/circle.py | 4 +- .../Lesson08/sparse_array/slice_sparse.py | 2 +- .../Lesson08/sparse_array/sparse_array.py | 2 +- 23 files changed, 106 insertions(+), 94 deletions(-) diff --git a/source/examples/context_managers/context_manager.py b/source/examples/context_managers/context_manager.py index 7e6f74c..127aea6 100644 --- a/source/examples/context_managers/context_manager.py +++ b/source/examples/context_managers/context_manager.py @@ -1,7 +1,7 @@ # Demo of a contextmanager -class Context(object): +class Context: """ from Doug Hellmann, PyMOTW https://pymotw.com/3/contextlib/#module-contextlib diff --git a/source/examples/metaprogramming/my_solution.py b/source/examples/metaprogramming/my_solution.py index 2605088..676c37e 100644 --- a/source/examples/metaprogramming/my_solution.py +++ b/source/examples/metaprogramming/my_solution.py @@ -1,5 +1,6 @@ # original class made as simple as possible -class TestClass(object): + +class TestClass: engine = None @@ -22,9 +23,9 @@ def get_privilege(self): return self.data, TestClass.engine -class MyClass(object): - - class __MyClass(object): +class MyClass: + + class __MyClass: def __init__(self, db_config): self.db_config = db_config self.data = None diff --git a/source/examples/metaprogramming/start_class.py b/source/examples/metaprogramming/start_class.py index dc8af24..acedb6f 100644 --- a/source/examples/metaprogramming/start_class.py +++ b/source/examples/metaprogramming/start_class.py @@ -1,7 +1,6 @@ import sqlalchemy - -class AccessTable(object): +class AccessTable: engine = None @@ -49,7 +48,7 @@ def get_privilege(self, name, group): access = self.metadata.tables['access'] select = sqlalchemy.select([access.c.privilege]).where(sqlalchemy.sql.and_(access.c.name == name, access.c.group == group)) - + # TODO handle result including case where it doesn't find any records _result = engine.execute(select) return 'readwrite' diff --git a/source/examples/multiple_inheritance/diamond.py b/source/examples/multiple_inheritance/diamond.py index b994c3a..f43919d 100644 --- a/source/examples/multiple_inheritance/diamond.py +++ b/source/examples/multiple_inheritance/diamond.py @@ -36,7 +36,7 @@ """ -class A(object): +class A: def do_your_stuff(self): print("doing A's stuff") diff --git a/source/examples/multiple_inheritance/diamond_super.py b/source/examples/multiple_inheritance/diamond_super.py index ca5d929..97bf87e 100644 --- a/source/examples/multiple_inheritance/diamond_super.py +++ b/source/examples/multiple_inheritance/diamond_super.py @@ -34,7 +34,7 @@ """ -class A(object): +class A: def do_your_stuff(self): print("doing A's stuff") diff --git a/source/examples/multiple_inheritance/mro.py b/source/examples/multiple_inheritance/mro.py index 760d70a..0bed35e 100644 --- a/source/examples/multiple_inheritance/mro.py +++ b/source/examples/multiple_inheritance/mro.py @@ -1,7 +1,7 @@ #!/usr/bin/env python -class A(object): +class A: def my_method(self): print("called A") diff --git a/source/examples/multiple_inheritance/super_test.ipynb b/source/examples/multiple_inheritance/super_test.ipynb index 86e9f62..30d23eb 100644 --- a/source/examples/multiple_inheritance/super_test.ipynb +++ b/source/examples/multiple_inheritance/super_test.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -74,7 +74,7 @@ "python3 fills these in for you at run time, but in python2, you needed to specify them:\n", "\n", "```\n", - "class A(object):\n", + "class A:\n", " def __init__(self):\n", " super(A, self).__init__()\n", "```\n", @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -115,7 +115,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -132,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -142,7 +142,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mD\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mD\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: super(type, obj): obj must be an instance or subtype of type" ] } @@ -160,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -178,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -196,7 +196,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -220,10 +220,8 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 9, + "metadata": {}, "outputs": [], "source": [ "# without super()\n", @@ -245,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -281,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -301,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -340,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -365,7 +363,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -396,10 +394,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": true - }, + "execution_count": 15, + "metadata": {}, "outputs": [], "source": [ "class Base():\n", @@ -424,7 +420,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -451,7 +447,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -460,7 +456,7 @@ "(__main__.A, __main__.Base, object)" ] }, - "execution_count": 20, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -480,7 +476,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -507,7 +503,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -540,9 +536,14 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, "outputs": [], "source": [] } @@ -563,7 +564,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.6" } }, "nbformat": 4, diff --git a/source/examples/multiple_inheritance/super_test.py b/source/examples/multiple_inheritance/super_test.py index 0a73e93..2202b48 100644 --- a/source/examples/multiple_inheritance/super_test.py +++ b/source/examples/multiple_inheritance/super_test.py @@ -50,7 +50,7 @@ def __init__(self): # specify them: # # ``` -# class A(object): +# class A: # def __init__(self): # super(A, self).__init__() # ``` diff --git a/source/examples/nosql/address_book_model.py b/source/examples/nosql/address_book_model.py index 6cc28cb..9a399c7 100644 --- a/source/examples/nosql/address_book_model.py +++ b/source/examples/nosql/address_book_model.py @@ -1,13 +1,13 @@ #!/usr/bin/env python """ -sample data for NOSQL examples +Sample data for NOSQL examples This version has a not completely-trival data model """ -class Person(object): +class Person: """ class to represent an individual person """ @@ -43,7 +43,7 @@ def __repr__(self): return self.__str__() -class Address(object): +class Address: """ class that represents an address """ @@ -70,11 +70,11 @@ def __str__(self): return msg -class Household(object): +class Household: """ Class that represents a Household. - A household has one or more people, and a Location + A household has one or more people, and an address """ def __init__(self, @@ -97,7 +97,7 @@ def __repr__(self): return self.__str__() -class Business(Household): +class Business: """ Class that represents a Business @@ -108,7 +108,7 @@ class Business(Household): pass -class AddressBook(object): +class AddressBook: """ And address book -- has people, households, businesses. diff --git a/source/examples/nosql/address_book_mongo.py b/source/examples/nosql/address_book_mongo.py index 9d0a96a..137c5b5 100644 --- a/source/examples/nosql/address_book_mongo.py +++ b/source/examples/nosql/address_book_mongo.py @@ -1,7 +1,7 @@ #!/usr/bin/env python """ -sample data for NOSQL examples +Sample data for NOSQL examples This version uses mongoDB to store the data. @@ -168,7 +168,7 @@ class Business(Household): pass -class AddressBook(object): +class AddressBook: """ An address book -- has people, households, businesses. @@ -223,7 +223,8 @@ def find_people(self, name=''): Find all the people with name in their name somewhere """ # fixme -- can this query be combined? - # like this: db.inventory.find( { $or: [ { qty: { $lt: 20 } }, { sale: true } ] } ) + # like this: + # db.inventory.find( { $or: [ { qty: { $lt: 20 } }, { sale: true } ] } ) cursor = self.people.find({"first_name": {'$regex': '.*' + name + '.*', '$options': 'i'}}) diff --git a/source/examples/pep8/listing1.py b/source/examples/pep8/listing1.py index e6f1014..c6cf489 100644 --- a/source/examples/pep8/listing1.py +++ b/source/examples/pep8/listing1.py @@ -11,7 +11,7 @@ float = 1.0 -long = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong" +long = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong" @@ -22,8 +22,8 @@ def functionName(self, int): module_variable = 5*5 return module_variable -class my_class(object): - +class my_class: + def __init__(self, arg1, string): self.value = True return @@ -35,20 +35,20 @@ def method1(self, str): def method2(self): return print('How did we get here?') - + def method1(self): return self.value + 1 method2 = method1 - + class my_subclass(my_class): - + def __init__(self, arg1, string): self.value = arg1 return -class Food(object): +class Food: pass class Pizza(Food): diff --git a/source/examples/wikidef/definitions.py b/source/examples/wikidef/definitions.py index 0976ec8..e3c7032 100644 --- a/source/examples/wikidef/definitions.py +++ b/source/examples/wikidef/definitions.py @@ -1,7 +1,7 @@ from api import Wikipedia -class Definitions(object): +class Definitions: @staticmethod def article(title): diff --git a/source/exercises/html_renderer/html_render.py b/source/exercises/html_renderer/html_render.py index 508400f..1e65941 100755 --- a/source/exercises/html_renderer/html_render.py +++ b/source/exercises/html_renderer/html_render.py @@ -6,7 +6,15 @@ # This is the framework for the base class -class Element(object): + +class Element: + """ + Base class for all HTML elements + + This is an "abstract" base class, it is not intended + to be used by itself, but only used as a template for + specific element subclasses + """ def __init__(self, content=None): pass @@ -15,4 +23,4 @@ def append(self, new_content): pass def render(self, out_file): - out_file.write("just something as a place holder...") + out_file.write("Just something as a place holder...") diff --git a/source/exercises/html_renderer/html_renderer_tutorial.rst b/source/exercises/html_renderer/html_renderer_tutorial.rst index 762688c..cdc2951 100644 --- a/source/exercises/html_renderer/html_renderer_tutorial.rst +++ b/source/exercises/html_renderer/html_renderer_tutorial.rst @@ -110,13 +110,13 @@ But this one failed: assert file_contents.startswith("") assert file_contents.endswith("") -OK -- this one really does something real -- it tries to render an html element -- which did NOT pass -- so it's time to put some real functionality in the Element class. +OK -- this one really does something real -- it tries to render an html element -- which did NOT pass -- so it's time to put some real functionality in the ``Element`` class. This is the code: .. code-block:: python - class Element(object): + class Element: def __init__(self, content=None): pass @@ -139,7 +139,7 @@ So we need to add a tiny bit of code: .. code-block:: python - class Element(object): + class Element: tag = "html" diff --git a/source/modules/BasicPython.rst b/source/modules/BasicPython.rst index ac6a9b3..751bbf4 100644 --- a/source/modules/BasicPython.rst +++ b/source/modules/BasicPython.rst @@ -108,12 +108,12 @@ Any Python object can be printed (though it might not be pretty...) .. code-block:: ipython - In [1]: class bar(object): + In [1]: class Bar: ...: pass ...: - In [2]: print(bar) - + In [2]: print(Bar) + Code Blocks ........... diff --git a/source/modules/ContextManagers.rst b/source/modules/ContextManagers.rst index 60e5d8c..db8ff18 100644 --- a/source/modules/ContextManagers.rst +++ b/source/modules/ContextManagers.rst @@ -135,8 +135,9 @@ Consider this code: .. code-block:: python - class Context(object): - """from Doug Hellmann, PyMOTW + class Context: + """ + from Doug Hellmann, PyMOTW https://pymotw.com/3/contextlib/#module-contextlib """ def __init__(self, handle_error): diff --git a/source/modules/MetaProgramming.rst b/source/modules/MetaProgramming.rst index efe62c0..c10c95c 100644 --- a/source/modules/MetaProgramming.rst +++ b/source/modules/MetaProgramming.rst @@ -487,8 +487,7 @@ same signature as type(): (``(name, bases, dict)``) **Python2 NOTE:** -In Python 2, instead of the keyword argument, a special class attribute: -``__metaclass__`` is used: +In Python 2, instead of the keyword argument, a special class attribute: ``__metaclass__`` is used: .. code-block:: python diff --git a/source/modules/PythonClasses.rst b/source/modules/PythonClasses.rst index 71acfd8..14b00d5 100644 --- a/source/modules/PythonClasses.rst +++ b/source/modules/PythonClasses.rst @@ -124,31 +124,33 @@ see: :download:`simple_classes.py <../examples/classes/simple_classes.py>` The Initializer --------------- -The ``__init__`` special method is known as the initializer. It is automatically called when a new instance of a class is created. +The ``__init__`` special method serves as the initializer for class instances. It is automatically called when a new instance of a class is created. You can use it to do any set-up you need: .. code-block:: python - class Point(object): + class Point: def __init__(self, x, y): self.x = x self.y = y -It gets the arguments passed when you call the class object: +It gets the arguments passed when you call the class object, e.g.: .. code-block:: python Point(x, y) -Once you have defined an __init__, you can create "instances" of the class: +Will call ``Point.__init__()`` with x and y as arguments. + +Once you have defined an ``__init__``, you can create "instances" of the class: .. code-block:: python - p = Point(3,4) + p = Point(3, 4) -And access the attributes: +And access its attributes: .. code-block:: python @@ -182,19 +184,19 @@ That's where all the instance-specific data is. Class Attributes ---------------- -In the above example, we assigned two attributes to ``self`` -- these are going to be different for each instance, or copy of this class. But what if you want all the instances of a class to share the same values? +In the above example, we assigned two attributes to ``self`` -- these are going to be different for each instance, or copy, of this class. But what if you want all the instances of a class to share the same values? .. code-block:: python - class Point(object): + class Point: size = 4 - color= "red" + color = "red" def __init__(self, x, y): self.x = x self.y = y Anything assigned in the class scope is a class attribute -- every -instance of the class shares the same one. +instance of the class shares the same one. So in this case, all Points will have the same size and color, but different x and y coordinates. Note: the methods defined by ``def`` are class attributes as well. @@ -215,21 +217,21 @@ So in this case, ``size`` and ``color`` are class attributes. But note in ``get_color`` -- it accesses color from ``self``: -class attributes are accessed with ``self`` also. +Class attributes are accessed with ``self`` also. So what is the difference? - * class attributes are shared by ALL the instances of the class. - * instance attributes are unique to each instance -- each one has its own copy. + * Class attributes are shared by ALL the instances of the class. + * Instance attributes are unique to each instance -- each one has its own copy. Example: .. code-block:: ipython In [6]: class C: - ...: x = [1,2,3] # class attribute + ...: x = [1, 2, 3] # class attribute ...: def __init__(self): - ...: self.y = [4,5,6] # instance attribute + ...: self.y = [4, 5, 6] # instance attribute ...: In [7]: c1 = C() diff --git a/source/solutions/Extras/persistance/circle.py b/source/solutions/Extras/persistance/circle.py index 24e40f5..74c6195 100644 --- a/source/solutions/Extras/persistance/circle.py +++ b/source/solutions/Extras/persistance/circle.py @@ -13,7 +13,7 @@ # this is a trick to make all the greater than, less than, etc work. # see: https://docs.python.org/3.5/library/functools.html#functools.total_ordering @functools.total_ordering -class Circle(object): +class Circle: def __init__(self, radius): self.radius = float(radius) diff --git a/source/solutions/Lesson07/version2/html_render.py b/source/solutions/Lesson07/version2/html_render.py index f9ff4ca..232fcad 100644 --- a/source/solutions/Lesson07/version2/html_render.py +++ b/source/solutions/Lesson07/version2/html_render.py @@ -4,7 +4,7 @@ # This is the framework for the base class -class Element(object): +class Element: tag = "html" indent = " " diff --git a/source/solutions/Lesson08/circle.py b/source/solutions/Lesson08/circle.py index 9c93ace..e604c73 100644 --- a/source/solutions/Lesson08/circle.py +++ b/source/solutions/Lesson08/circle.py @@ -1,7 +1,7 @@ #!/usr/bin/env python """ -nifty Circle class +Nifty Circle class Used to demo propeties and "magic methods" """ @@ -13,7 +13,7 @@ # this is a trick to make all the greater than, less than, etc work. # see: https://docs.python.org/3.6/library/functools.html#functools.total_ordering @functools.total_ordering -class Circle(object): +class Circle: """ simple class to represent a circle diff --git a/source/solutions/Lesson08/sparse_array/slice_sparse.py b/source/solutions/Lesson08/sparse_array/slice_sparse.py index c40fb76..27b40bd 100644 --- a/source/solutions/Lesson08/sparse_array/slice_sparse.py +++ b/source/solutions/Lesson08/sparse_array/slice_sparse.py @@ -4,7 +4,7 @@ """ -class SparseArray(object): +class SparseArray: def __init__(self, my_array=()): self.length = len(my_array) diff --git a/source/solutions/Lesson08/sparse_array/sparse_array.py b/source/solutions/Lesson08/sparse_array/sparse_array.py index d9543ca..4ce8ddf 100644 --- a/source/solutions/Lesson08/sparse_array/sparse_array.py +++ b/source/solutions/Lesson08/sparse_array/sparse_array.py @@ -10,7 +10,7 @@ """ -class SparseArray(object): +class SparseArray: def __init__(self, my_array=()): """