<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>codehop &#187; inheritance</title>
	<atom:link href="http://codehop.com/tag/inheritance/feed/" rel="self" type="application/rss+xml" />
	<link>http://codehop.com</link>
	<description>#code #art #music</description>
	<lastBuildDate>Mon, 23 Apr 2012 18:37:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Operators Made Easy</title>
		<link>http://codehop.com/operators-made-easy/</link>
		<comments>http://codehop.com/operators-made-easy/#comments</comments>
		<pubDate>Sat, 01 May 2010 18:36:14 +0000</pubDate>
		<dc:creator><![CDATA[Jacob Joaquin]]></dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[operator overloading]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unit generator]]></category>

		<guid isPermaLink="false">http://slipmat.noisepages.com/?p=223</guid>
		<description><![CDATA[There was something that seriously annoyed me about yesterday&#8217;s instrument graph. The use of the Multiply() and the Sum() is bothersome; I&#8217;m used to expressing this functionality in a more concise manner using the * and + operators. Download today&#8217;s &#8230; <a href="http://codehop.com/operators-made-easy/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>There was something that seriously annoyed me about yesterday&#8217;s <a href="http://slipmat.noisepages.com/2010/04/instruments-simplified-with-python-decorators/">instrument graph</a>. The use of the Multiply() and the Sum() is bothersome; I&#8217;m used to expressing this functionality in a more concise manner using the * and + operators. Download today&#8217;s code <a href="http://snipt.org/Mgno">here</a>.</p>
<p>This doesn&#8217;t work for me:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
return Multiply(Sum(a1, a2), RiseFall(dur, 0.5))
</pre>
<p>Thankfully, Python allows us to <a href="http://en.wikipedia.org/wiki/Operator_overloading">overload the operators</a>, so we can express the same line as this:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
return (a1 + a2) * RiseFall(dur, 0.5)
</pre>
<p>Less typing, easier to read. Let&#8217;s see it in the context of @Instr MyInstr:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
@Instr
def MyInstr(dur=1.0, amp=1.0, freq=440, tune=12):
    a1 = Sine(amp * 0.5, freq)
    a2 = Sine(amp * 0.5, freq * 2 ** (tune / 12.0))
    return (a1 + a2) * RiseFall(dur, 0.5)
</pre>
<p>Here&#8217;s how I implemented it. I started by creating a generic iterator class called UnitGenerator:</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
class UnitGenerator:
    def __init__(self): pass
    def __iter__(self): pass                 
    def next(self): raise StopIteration    
    def __add__(self, i): return Add(self, i)
    def __mul__(self, i): return Mul(self, i)
</pre>
<p>The last two lines of the class redefine __add__() and __mul__(), which control the behaviors of + and *.  These functions use the custom classes Add() and Mul().  These were originally called Sum() and Multiply(), though I renamed them to follow Python naming conventions. The last thing I had to do was alter some of the existing classes to derive from class UnitGenerator, so they automatically incorporate the overloaded operators.</p>
<pre style="font-family: 'Courier New', courier, monaco, monospace, sans-serif; font-size: 1.2em; padding-bottom: 16px">
class Instr(UnitGenerator):  ...
class IterReduce(UnitGenerator):  ...
class Mul(IterReduce):  ...
class Add(IterReduce):  ...
class RiseFall(UnitGenerator):  ...
class Sine(UnitGenerator):  ...
</pre>
<p>Classes Mul and Add are also of type UnitGenerator. They inherit for class IterReduce which inherents from UnitGenerator.</p>
]]></content:encoded>
			<wfw:commentRss>http://codehop.com/operators-made-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
