From charlesreid1

(Created page with "==Redefining an Object== Scenario: you are defining an object, and want to extend it by adding additional methods. ===How not to do it=== The following method will not work...")
 
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 3: Line 3:
Scenario: you are defining an object, and want to extend it by adding additional methods.
Scenario: you are defining an object, and want to extend it by adding additional methods.


===How not to do it===
The trick: in the first file, we define a class called <code>EchoBase</code> with the following line:


The following method will not work - each time you redefine the object in a subsequent file, all definitions in prior files are lost.
<pre>
class EchoBase(object):
</pre>
 
and in subsequent classes, we extend this definition without overwriting it by re-defining <code>EchoBase</code> as follows:
 
<pre>
class EchoBase(EchoBase):
</pre>


A very simple python package:
Here is a very simple python package to provide an example:


<pre>
<pre>
Line 18: Line 26:


4 files
4 files
$ cat pkg_test/__init__.py
from .a import *
from .b import *
from .c import *




Line 32: Line 46:
from .a import EchoBase
from .a import EchoBase


class EchoBase(object):
class EchoBase(EchoBase):
     def bar(self):
     def bar(self):
         print('bar')
         print('bar')
Line 38: Line 52:


$ cat pkg_test/c.py
$ cat pkg_test/c.py
from .a import EchoBase
from .b import EchoBase


class EchoBase(object):
class EchoBase(EchoBase):
     def buz(self):
     def buz(self):
         print('buz')
         print('buz')
</pre>
</pre>
IMPORTANT: Also note that c imports from b, so that their definitions are chained. If c imports from a, it overwrites changes that b makes.


Now what we can do is, from the directory where the <code>pkg_test</code> directory is located, open a python prompt, and create an <code>EchoBase</code> object:
Now what we can do is, from the directory where the <code>pkg_test</code> directory is located, open a python prompt, and create an <code>EchoBase</code> object:
Line 58: Line 74:


>>> e.foo()
>>> e.foo()
Traceback (most recent call last):
foo
  File "<stdin>", line 1, in <module>
AttributeError: 'EchoBase' object has no attribute 'foo'


>>> e.bar()
>>> e.bar()
Traceback (most recent call last):
bar
  File "<stdin>", line 1, in <module>
AttributeError: 'EchoBase' object has no attribute 'bar'


>>> e.buz()
>>> e.buz()
Line 73: Line 85:


Only the method defined in <code>c.py</code>, the last file imported, is defined in the end.
Only the method defined in <code>c.py</code>, the last file imported, is defined in the end.
==Flags==
[[Category:Python]]
[[Category:Python Packaging]]
[[Category:Packaging]]

Latest revision as of 16:09, 15 November 2018

Redefining an Object

Scenario: you are defining an object, and want to extend it by adding additional methods.

The trick: in the first file, we define a class called EchoBase with the following line:

class EchoBase(object):

and in subsequent classes, we extend this definition without overwriting it by re-defining EchoBase as follows:

class EchoBase(EchoBase):

Here is a very simple python package to provide an example:

$ tree pkg_test/
pkg_test/
├── __init__.py
├── a.py
├── b.py
└── c.py

4 files


$ cat pkg_test/__init__.py
from .a import *
from .b import *
from .c import *


$ cat pkg_test/a.py
class EchoBase(object):
    def __init__(self):
        pass

    def foo(self):
        print('foo')


$ cat pkg_test/b.py
from .a import EchoBase

class EchoBase(EchoBase):
    def bar(self):
        print('bar')


$ cat pkg_test/c.py
from .b import EchoBase

class EchoBase(EchoBase):
    def buz(self):
        print('buz')

IMPORTANT: Also note that c imports from b, so that their definitions are chained. If c imports from a, it overwrites changes that b makes.

Now what we can do is, from the directory where the pkg_test directory is located, open a python prompt, and create an EchoBase object:

$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct  6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import pkg_test

>>> e = pkg_test.EchoBase()

>>> e.foo()
foo

>>> e.bar()
bar

>>> e.buz()
buz

Only the method defined in c.py, the last file imported, is defined in the end.


Flags