Skip to content

Instantly share code, notes, and snippets.

@k0emt
Created September 2, 2011 20:15
Show Gist options
  • Save k0emt/1189771 to your computer and use it in GitHub Desktop.
Save k0emt/1189771 to your computer and use it in GitHub Desktop.
Overloading constructor and variable arguments example
__author__ = 'k0emt'
class Greeter:
def __init__(self):
self.message = 'Hello world!'
# print self.message
class GreeterOverloaded:
def __init__(self, greeting=None):
if greeting is None:
self.myGreeting = 'Hello World!'
else:
self.myGreeting = greeting
def greeting(self):
return self.myGreeting
class GreeterOverloadedWithOr:
def __init__(self, greeting=None):
self.myGreeting = greeting or 'Zoom!'
def greeting(self):
return self.myGreeting
class GreeterOverloadedWithVariableArgs:
def __init__(self, *args ):
self.myGreeting = 'meh'
if len(args) > 0:
self.myGreeting = ''
for arg in args:
self.myGreeting += arg + ' '
self.myGreeting = self.myGreeting.strip()
def greeting(self):
return self.myGreeting
class GreeterOverloadedWithKeywordArgs:
def __init__(self, **kwargs):
self.myGreeting = 'Have a nice day!'
if len(kwargs) > 0:
self.myGreeting = kwargs['part1'] + kwargs['part2']
def greeting(self):
return self.myGreeting
# a good reference for getting and sending arguments can be found here:
# http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
__author__ = 'k0emt'
import unittest
from Arguments import *
#from Experiment import GreeterOverloaded
class MyTestCase(unittest.TestCase):
def test_default_greeting_set(self):
greeter = Greeter()
self.assertEqual(greeter.message, 'Hello world!')
class GreeterTest(unittest.TestCase):
def test_greetingReturnsDefault(self):
mw = GreeterOverloaded()
self.assertEqual(mw.greeting(),'Hello World!','greeting is not as expected')
def test_greetingReturnsConstructorProvidedContent(self):
mw = GreeterOverloaded('booyah!')
self.assertEqual(mw.greeting(),'booyah!')
def test_greetingReturnsConstructorProvidedContentArgsDemo(self):
myArgument = {'arrrrr'}
mw = GreeterOverloaded(*myArgument)
self.assertEqual(mw.greeting(),'arrrrr')
class GreeterOverloadedWithOrTest(unittest.TestCase):
def test_greetingReturnsDefault(self):
mw = GreeterOverloadedWithOr()
self.assertEqual(mw.greeting(),'Zoom!','greeting is not as expected')
def test_greetingReturnsConstructorProvidedContent(self):
mw = GreeterOverloadedWithOr('Yeah')
self.assertEqual(mw.greeting(),'Yeah')
class GreeterOverloadedWithVariableArgsTest(unittest.TestCase):
def test_greetingReturnsDefault(self):
mw = GreeterOverloadedWithVariableArgs()
self.assertEqual(mw.greeting(),'meh')
def test_greetingReturnsConstructorProvidedContent(self):
mw = GreeterOverloadedWithVariableArgs('Hello','beautiful','world')
self.assertEqual(mw.greeting(),'Hello beautiful world')
class GreeterOverloadedWithKeywordArgsTest(unittest.TestCase):
def test_greetingReturnsDefault(self):
mw = GreeterOverloadedWithKeywordArgs()
self.assertEqual(mw.greeting(),'Have a nice day!')
def test_greetingReturnsKeywordArgsProvidedContent(self):
mw = GreeterOverloadedWithKeywordArgs(part1='One ', part2='two')
self.assertEqual(mw.greeting(),'One two')
def test_greetingReturnsKeywordArgsProvidedContentDemo(self):
myArguments = {'part1':'Good ', 'part2':'Luck!'}
mw = GreeterOverloadedWithKeywordArgs(**myArguments)
self.assertEqual(mw.greeting(),'Good Luck!')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment