[Tutor] Extending Classes, Instantiation, and attribute errors (Code Attached)

Lloyd Kvam pythontutor@venix.com
Mon, 13 May 2002 09:13:43 -0400


As a first step, remove the __init__(self):	pass

You only need __init__ if it does something useful.  If you
do write an __init__ function, it becomes YOUR responsibility to
invoke the parent __init__.  Otherwise, the Python Runtime will
invoke the correct parent __init__'s automatically.

Your child object is also an instance of the parent classes.  It
will normally have all of the parent attributes unless you do
something to prevent that.  In this case your __init__: pass
prevents the child from getting the rxd and sent attributes that
would be created by the parent __init__

Steve wrote:

> NOTE: Code at he bottom of the post, also this is a
> repost, I posted late last night and I think my vague
> subject might have caused many to disreguard the post.
>  
> If you've already seen this sorry for the repost! 
> 
>  Lo all,
>  
>    I'm trying to learn python and just get this code
> to
> connect and perhaps send a few lines to the server I
> realize its incomplete. I've been having troubles
> figuring out a few things. First off if I want a
> variable to be shared between classes, and i've made
> the child class extend the parent, where do I place my
> multiclass variables? If I put then in the __init__ of
> the parent class and don't call the class directly it
> seems as if that method isn't called? I'm also not
> sure how instantiation works between classes, in my
> test function below don't I instantiate the class with
> my assignment to c? and when  I open a socket doesn't
> sock instantiate the call? I'm also unsure about all
> the selfs I seem to be unsure as to when I need to
> call the class.method, self.method etc... (a few of
> the books i'm reading call class.method when one
> method in a
> class calls another in the same class) any help would
> be greately appreciated. Seems the latest error I was
> getting was:
>    
>   AttributeError: ircConnect instance has no attribute
> 'send'
>  
> This I also don't understand considering I import
> socket at the top of the file so I figured self.send
> would be a method of the socket class but apparently
> its not working either. I'm reading all I can and
> slowly moving up the learning curver sorry for the
> bother. 
> 
>                    Steve (l8tr2000@yahoo.com) 
> 
> PS. Thanks in advance for the help
> 
> #!/usr/bin/python
> 
> import sys, time
> from select import select
> from socket import socket, AF_INET, SOCK_STREAM 
> 
> class selectSocket:
>     def __init__(self):
>         self.rxd  = ''
>         self.sent = ''
> 
>     def connServer(self, host, port):
>         readsocks, writesocks = [], []
>         self.sock = socket(AF_INET, SOCK_STREAM)
>         self.sock.connect((host, port))
>         readsocks.append(self.sock)
>         writesocks.append(self.sock)
>         
>     def selectLoop(self):
>         print 'select-server loop starting'
>     
>         while 1:
>             r, w, e = select(readsocks, writesocks,
> [])
>             
>             for sockobj in r:
>                 self.rxd = sockobj.recv(1024)
>                 print 'DEBUG::', self.rxd, 'on',
> id(sockobj)
>                 
>                 if not self.rxd:        # if closed by
> the clients
>                     sockobj.close()      # close here
> and remove
>                     r.remove(sockobj)    # else
> reselected
>                 
>             for sockobj in w:
>             
>                 if newConn == 1:
>                     pass
>                     
>                 else:
>                     pass
> 
>         time.sleep(.5)
>         
> class ircConnect(selectSocket):
>     def __init__(self):
>         pass
> 
>     def connIRC(self, host, port, chan, nick, name):
>         self.connServer(host, port)
>         self.ircReg(nick, name)
>         self.joinChan(self, chan)
>     
>     def ircReg(self, nick, name):
>         self.send('NICK', nick)
>         self.send('USER', nick, '+iw', name)
>         self.send('PONG\n')
>         
>     def joinChan(self, chan):
>         self.send('JOIN', chan)
>         
> def test(host, port, chan, nick, name):
>     c = ircConnect()
>     c.connIRC(host, port, chan, nick, name)
>     c.selectLoop()
> 
> if __name__=='__main__':
> 
>     test('irc.openprojects.net', 6667, '#nix',
> 'testmoe', "testmoe neener")  
> 
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> LAUNCH - Your Yahoo! Music Experience
> http://launch.yahoo.com
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582