Re: [Tutor] problem with a sub-class

2017-12-01 Thread Sydney Shall

On 30/11/2017 22:08, Alan Gauld via Tutor wrote:

On 30/11/17 15:37, Shall, Sydney wrote:


My problem is with constructing a sub-class.

My sub-class is constructed as follows:

import Population_ProductivityV24 as POCWP


Note that POCWP is an alias for the *module* Population_ProductivityV24.
It is not a class.


line 27 : class SimulateCycleZero(POCWP):


Here you define a class that subclasses your imported module.
Frankly I'm surprised that you don't get an error there
but hey...


line 28 : def __init__(self, dirname_p):


But this is now an init for a subclass of module.


The error message is as follows:

File
"/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py",
line 27, in 
  class SimulateCycleZero(POCWP):

TypeError: module.__init__() takes at most 2 arguments (3 given)


So I'm guessing the use of a module to subclass
has confused things and I confess I'm not clear on
exactly what is going on and why you get this message.
But I'm pretty sure you don;t want to subclass your
imported module and thats the mistake.




Thanks to Alan and Peter for explaining sub-classing to me. I understand 
a bit better now. My program is corrected and does not give the error 
any more.



--
Sydney
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with a sub-class

2017-12-01 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 30/11/17 15:37, Shall, Sydney wrote:
> 
>> My problem is with constructing a sub-class.
>> 
>> My sub-class is constructed as follows:
>> 
>> import Population_ProductivityV24 as POCWP
> 
> Note that POCWP is an alias for the *module* Population_ProductivityV24.
> It is not a class.
> 
>> line 27 : class SimulateCycleZero(POCWP):
> 
> Here you define a class that subclasses your imported module.
> Frankly I'm surprised that you don't get an error there
> but hey...
> 
>> line 28 : def __init__(self, dirname_p):
> 
> But this is now an init for a subclass of module.
> 
>> The error message is as follows:
>> 
>>File
>> 
"/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py",
>> line 27, in 
>>  class SimulateCycleZero(POCWP):
>> 
>> TypeError: module.__init__() takes at most 2 arguments (3 given)
> 
> So I'm guessing the use of a module to subclass
> has confused things and I confess I'm not clear on
> exactly what is going on and why you get this message.

A class is an instance of its metaclass.

class A:
pass

is roughly equivalent to

A = type("A", (), {}) # classname, base classes, class attributes

and

class B(A):>>> class A:
... def __init__(self, *args):
... print("__init__{}".format(args))
... 
>>> class B(A()): pass
... 
__init__()
__init__('B', (<__main__.A object at 0x7f3db8a1c048>,), {'__module__': 
'__main__', '__qualname__': 'B'})
>>> assert isinstance(B, A)
>>> isinstance(B, A)
>>> 
>>> 
True

   
>>> B() 
>>> 
>>>
Traceback (most recent call last):  

   
  File "", line 1, in

   
TypeError: 'A' object is not callable   

   
foo = 42

is roughly equivalent to

B = type(A)("B", (A,), {"foo": 42})

When you subclass from an instance of A instead of A itself this becomes

a = A()
B = type(a)("B", (a,), {"foo": 42})

which can be simplified to

B = A("B", (a,), {"foo": 42})

If this succeeds B is bound to an instance of A, but usually you'll see a 
TypeError, either immediately as the OP, 

>>> class A: pass
... 
>>> class B(A()): pass
... 
Traceback (most recent call last):
  File "", line 1, in 
TypeError: object() takes no parameters

or later when you try to instantiate B:

>>> class A:
... def __init__(self, *args):
... print("__init__{}".format(args))
... 
>>> class B(A()): pass
... 
__init__()
__init__('B', (<__main__.A object at 0x7f3db8a1c048>,), {'__module__': 
'__main__', '__qualname__': 'B'})
>>> isinstance(B, A)
>>> 
>>> 
True

   
>>> B() 
>>> 
>>>
Traceback (most recent call last):  

   
  File "", line 1, in

   
TypeError: 'A' object is not callable   

   

> But I'm pretty sure you don;t want to subclass your
> imported module and thats the mistake.


__

Re: [Tutor] problem with a sub-class

2017-11-30 Thread Alan Gauld via Tutor
On 30/11/17 15:37, Shall, Sydney wrote:

> My problem is with constructing a sub-class.
> 
> My sub-class is constructed as follows:
> 
> import Population_ProductivityV24 as POCWP

Note that POCWP is an alias for the *module* Population_ProductivityV24.
It is not a class.

> line 27 : class SimulateCycleZero(POCWP):

Here you define a class that subclasses your imported module.
Frankly I'm surprised that you don't get an error there
but hey...

> line 28 : def __init__(self, dirname_p):

But this is now an init for a subclass of module.

> The error message is as follows:
> 
>File 
> "/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py",
>  
> line 27, in 
>  class SimulateCycleZero(POCWP):
> 
> TypeError: module.__init__() takes at most 2 arguments (3 given)

So I'm guessing the use of a module to subclass
has confused things and I confess I'm not clear on
exactly what is going on and why you get this message.
But I'm pretty sure you don;t want to subclass your
imported module and thats the mistake.


-- 
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] problem with a sub-class

2017-11-30 Thread Shall, Sydney

I am almost a beginner.
I have an error message which I cannot understand.
My problem is with constructing a sub-class.

I use;
MAC OS V10.13.1
Anaconda Python 3.5

My base Class works properly and all 136 tests of the Base Class are 
correct.


My sub-class is constructed as follows:

import sys
import random
import numpy as np
import pylab
import copy
import Population_InitV8 as POCI
import Population_ProductivityV24 as POCWP

line 27 : class SimulateCycleZero(POCWP):
line 28 : def __init__(self, dirname_p):

The program follows on from this.

The error message is as follows:


  File 
"/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py", 
line 27, in 

class SimulateCycleZero(POCWP):

TypeError: module.__init__() takes at most 2 arguments (3 given)


My problem is that I do not understand to which file the word 'module' 
in the error message applies.


It seems to me that somewhere I am providing 3 arguments when only a 
maximum of two are required.


When I do the following, I get the same error message.

line 27 : class SimulateCycleZero(POCWP):
line 28 : def __init__(self):

I would appreciate some guidance , please.

Sydney
_

Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor