Newbie Question regarding __init__()

S

Simon

Hi

I want to create an instance of dcCursor which inherits from
dcObject. When I run the following code it gives the error shown.
Can some explain to me what is wrong? I have included the dcObject.py
and dcCursor.py below.
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: __init__() takes no arguments (1 given)

# -*- coding: utf-8 -*-
# dcCursor.py - The base Cursor Class

from dcObject import dcObject

class dcCursor(dcObject):
"""The base Cursor Class"""
def OpenCursor(tcTemp,tcTable):
"""Opens the specified cursor
Parameters: tcTemp,tcTable"""
pass

def CreateCursor(tcTable):
"""Creates the specified cursor
Parameters: tcTable"""
pass

def init_Exec():
print("init_Exec called")



# -*- coding: utf-8 -*-
# dcObject.py - Base Object
class dcObject(object):
"""Base Object"""
def __init__():
"""default init method has the form
of init_Pre() and init_Exec
init_Post()"""
self.init_Pre() and self.init_Exec()
self.init_Post()

def init_Pre():
"""Always called before init_Exec()
if it returns false init_Exec is
not executed"""
return True

def init_Exec():
"""Base __init__ code goes here and this is
only executed if init_Pre() returns true"""
return True

def init_Post():
"""Always called after the init_Pre() and
init_Exec()"""
return True
 
M

MRAB

Simon said:
Hi

I want to create an instance of dcCursor which inherits from
dcObject. When I run the following code it gives the error shown.
Can some explain to me what is wrong? I have included the dcObject.py
and dcCursor.py below.

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: __init__() takes no arguments (1 given)
All instance methods must have the instance as the first argument, by
convention called 'self':
# -*- coding: utf-8 -*-
# dcCursor.py - The base Cursor Class

from dcObject import dcObject

class dcCursor(dcObject):
"""The base Cursor Class"""
def OpenCursor(tcTemp,tcTable):
def OpenCursor(self, tcTemp, tcTable):
"""Opens the specified cursor
Parameters: tcTemp,tcTable"""
pass

def CreateCursor(tcTable):
def CreateCursor(self, tcTable):
....

and so on, including the '__init__' method.
 
S

Simon

Hi

So should the dcObject class include the "self" as well since I have
not defined an __init__ method in dcCursor?

Simon
 
G

Gabriel Genellina

So should the dcObject class include the "self" as well since I have
not defined an __init__ method in dcCursor?

Every method that you define takes "self" as its first argument.
Every method that you want to call on the current instance must be
qualified by self.methodname(argu, ments) -- it is not different from
anotherobject.methodname(...)
Every attribute that you want to access from the current instance must be
qualified too: self.attributename
There is no implicit self/this in Python as in other languages.
 
G

Gabriel Genellina

One other thing. I'm a little confused by the first line of
dcObject.__init__:

self.init_Pre() and self.init_Exec()

I suspect this does not do what you think it does. init_Pre and
init_Exec
will both be called by this expression (unless init_Pre throws an
exception,
of course). You're not getting anything here that you wouldn't by just
calling each method on a separate line, except just making it harder to
read.

Well, perhaps Simon didn't grasp Python's syntax and semantics very well
yet, but his code would work as designed after fixing obvious errors:

class dcObject(object):
"""Base Object"""
def __init__(self):
"""default init method has the form
of init_Pre() and init_Exec
init_Post()"""
self.init_Pre() and self.init_Exec()
self.init_Post()

def init_Pre(self):
"""Always called before init_Exec()
if it returns false init_Exec is
not executed"""
return True

def init_Exec(self):
"""Base __init__ code goes here and this is
only executed if init_Pre() returns true"""
return True

def init_Post(self):
"""Always called after the init_Pre() and
init_Exec()"""
return True

init_Pre might return False, in that case init_Exec would not be executed.
init_Post is always called. And that's exactly what the docstrings say.

I would use an `if` statement instead:
if self.init_Pre(): self.init_Exec()
and make init_Exec and init_Post not return anything if they have no
intrinsic meaning. And probably use better names. But basically the
structure is OK.
 
D

Dave Angel

Nat said:
As MRAB described, ALL instance methods need to accept 'self' as a first
parameter, as that will be passed to them implicitly when they are called.
This includes __init__. The name 'self' is just a commonly accepted
convention for the name of the instance object passed to methods. You don't
have to call it that, but you really should.

Take a look at http://docs.python.org/tutorial/classes.html#class-objects
It might help shed some light on how methods and instances work.

One other thing. I'm a little confused by the first line of
dcObject.__init__:

self.init_Pre() and self.init_Exec()

I suspect this does not do what you think it does. init_Pre and init_Exec
will both be called by this expression (unless init_Pre throws an exception,
of course). You're not getting anything here that you wouldn't by just
calling each method on a separate line, except just making it harder to
read.
Read the doc-string for init_Pre() and for init_Exec(). The final
version of init_Pre() will return False in some circumstances, and in
those circumstances Simon doesn't want init_Exec() to be called. He's
deliberately using the short-circuit evaluation of 'and' to accomplish
that.
Hi

So should the dcObject class include the "self" as well since I have
not defined an __init__ method in dcCursor?

Simon
Every one of those methods in both of those classes need a "self" first
argument. As others have said, all instance methods need a 'self.'
 
S

Simon

Okay I will fix my code and include "self" and see what happens. I
know I tried that before and got another error which I suspect was
another newbie error.

The idea behind the init_Pre is that I can put custom code here to
customize the __init__ instead of creating a new subclass. This kind
of hook pattern allows you to flatten your inheritance hierarchy. I
can choose in the init_Pre method to execute code before the init_exec
(which contains the default __init__ code) and still execute the
init_Exec method or I can completely customize the entire __init__ by
returning False from init_Pre and prevent the init_Exec from being
called. I use this type of pattern with almost all my methods. In
this way I can create a less complicated inheritance chain but still
have have custom code when needed without sub-classing.

I am use to Visual FoxPro which where you can do

=This.init_Pre().And.This.init_Exec() and the result is discarded so
that is why it looks the way it does. In this form init_Exec has to
return a value. However, If self.init_Pre(): self.init_Exec() would
work the same and then I could avoid returning a value.

Thanks,
Simon
 
D

Dave Angel

Simon said:
Okay I will fix my code and include "self" and see what happens. I
know I tried that before and got another error which I suspect was
another newbie error.

The idea behind the init_Pre is that I can put custom code here to
customize the __init__ instead of creating a new subclass. This kind
of hook pattern allows you to flatten your inheritance hierarchy. I
can choose in the init_Pre method to execute code before the init_exec
(which contains the default __init__ code) and still execute the
init_Exec method or I can completely customize the entire __init__ by
returning False from init_Pre and prevent the init_Exec from being
called. I use this type of pattern with almost all my methods. In
this way I can create a less complicated inheritance chain but still
have have custom code when needed without sub-classing.

I am use to Visual FoxPro which where you can do

=is.init_Pre().And.This.init_Exec() and the result is discarded so
that is why it looks the way it does. In this form init_Exec has to
return a value. However, If self.init_Pre(): self.init_Exec() would
work the same and then I could avoid returning a value.

Thanks,
Simon
(Please don't top-post. You should put new responses at the end of
quoted text, or sometimes inline if that's clearer.)

I don't understand your comparison to Foxpro. read on.

As your code was last posted, you don't need a return value from
init_Exec() Every function that doesn't have an explicit return will
return None. And None is interpreted as False in an "and" expression.
If you had an "if" around the whole thing, then you'd care.

DaveA
 
S

Simon

(Please don't top-post.  You should put new responses at the end of
quoted text, or sometimes inline if that's clearer.)

I don't understand your comparison to Foxpro.  read on.

As your code was last posted, you don't need a return value from
init_Exec()  Every function that doesn't have an explicit return will
return None.  And None is interpreted as False in an "and" expression.  
If you had an "if" around the whole thing, then you'd care.

DaveA

All I meant by the FoxPro comment was the idea of using the equal sign
without a variable to throw away the result. Also in FoxPro there is
no such thing as automatically returning None. If there is no
explicit return then True is returned.

Thanks I did not know that None is interpreted as False.

Simon
 
D

Dave Angel

Simon said:
All I meant by the FoxPro comment was the idea of using the equal sign
without a variable to throw away the result. Also in FoxPro there is
no such thing as automatically returning None. If there is no
explicit return then True is returned.

Thanks I did not know that None is interpreted as False.

Simon
To throw away the result of an expression in Python is even easier.
Just don't use it.
func1() and func2()
is a valid expression whose result is not used. And func2()'s result is
therefore irrelevant. But shortcircuiting means that func2() is only
called if func1() returned False (or something equivalent to it, like 0
or an empty list)
 
D

Dennis Lee Bieber

To throw away the result of an expression in Python is even easier.
Just don't use it.
func1() and func2()
is a valid expression whose result is not used. And func2()'s result is
therefore irrelevant. But shortcircuiting means that func2() is only
called if func1() returned False (or something equivalent to it, like 0
or an empty list)

Backwards...

When using "and", the RHS is only evaluated if the LHS evaluates to
TRUE (if the LHS is FALSE, why go further, since False and anything ->
False).

Conversely, when using "or", the RHS is only evaluated if the LHS is
FALSE.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Simon

To throw away the result of an expression in Python is even easier.  
Just don't use it.
       func1() and func2()
is a valid expression whose result is not used.  And func2()'s result is
therefore irrelevant.  But shortcircuiting means that func2() is only
called if func1() returned False (or something equivalent to it, like 0
or an empty list)

Thanks for telling me how Python throws away the result. Secondly my
code works perfectly now that I am properly defining my methods using
"self".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top