[Tutor] Do not understand code snippet from "26.8. test — Regression tests package for Python"

Alan Gauld alan.gauld at yahoo.co.uk
Sat Apr 15 19:31:50 EDT 2017


On 16/04/17 00:17, boB Stepp wrote:

> --------------------------------------------------------------------------------------
> #!/usr/bin/env python3
> 
> def mySuperWhammyFunction(any_input):
>     return any_input

This is a simple function, its not bound to an object

> 
> import unittest
> 
> class TestFuncAcceptsSequencesMixin:
> 
>     func = mySuperWhammyFunction
> 
>     def test_func(self):
>         self.func(self.arg)

This is calling self.function which implies a method.

Convert your function to a method and it should work.

> ERROR: test_func (test_super.AcceptLists)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "c:\Projects\test_super.py", line 13, in test_func
>     self.func(self.arg)
> TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given

The missing self parameter...

> I suspect that both an object instance and self.arg is getting passed

Its self.
When you do

object.method()

object gets passed as the first parameter (traditionally
called self) But because your function is not a method
it does not expect a self to be passed.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list