Class interaction

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Class interaction

 
0
  #1
Oct 23rd, 2008
I've gotten a little bit furter in understanding the general workings of python, and currently I'm fidgeting with classes and objects. I was thinking of the possibilities of having a program with a whole bunch of user defined classes that the user can use (hey, almost a repetitive aliteration there...) interactively.
So if I have a while loop that continously brings back a starting prompt, the user can write the class name followd by a suitable string or something, looking something like this:

new input> add H.Umbug
H Umbug just joined.

I've managed to get this version working, which I think gives an idea of what I'm after:

class add:
    def __init__(self, name):
        self.name = name
    def Hi(self):
        print self.name, "just joined"
p = add(raw_input("add "))
p.Hi()

However, as you can see this doesn't quite land where I want to. Any tips on where to start, if this is even possible, would be great!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 925
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Class interaction

 
0
  #2
Oct 23rd, 2008
So you want the user to be able to specify which class to use?
Well a way to do that would be to split the string and do some if statements such as:
  1. i = raw_input()
  2. if i.split()[0] == 'add':
  3. p = add(i)
  4. p.hi
If thats not it could you just maybe clarify it for us.
Thanks
Last edited by Paul Thompson; Oct 23rd, 2008 at 7:12 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: Class interaction

 
0
  #3
Oct 23rd, 2008
Haha, great forum - write a dumb question, wait an hour, get a good answer. That seems to be what I'm after, but of course I always have a few more dumb questions.

So if I write the whole program as it is I would have thought it should look like this:
class add:
    def __init__(self, name):
        self.name = name
    def Hi(self):
        print self.name, "just joined"
while True:                          #just for smoother testing
    i = raw_input("command>")
    if i.split()[0] == 'add':
        p = add(i.split()[1])      #I don't want add to take the word 
                                            "add" too, right?
        print p.Hi                     #This prints the location... 
     else:
        print "no"                     #Just for the sake of testing

Well, if I start up the program and do like this
command>add Swaroop
I'd expect the output to be "Swaroop just joined"
Instead I get <__main__.add instance at 0x00A98FD0>
Um... why?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,057
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 266
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Class interaction

 
0
  #4
Oct 23rd, 2008
Originally Posted by Devlan View Post
  1. print p.Hi #This prints the location...
get <__main__.add instance at 0x00A98FD0>
Um... why?
Because you asked for it

  1. print p.Hi() #This prints the location...
HTH
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: Class interaction

 
0
  #5
Oct 23rd, 2008
Ah, sub-christ, that hurt. Well, I'm learning, I'm learning. Thanks!

Right, so with your assistance I've managed to get the program I'm working on functional. Just one little detail, which is apparent in this example as well.
I print
command> add Swaroop

It answers
Swaroop just joined
None

Where does the None come from? What does that have to do with anything? Is it the while loop that returns it, is it something in the class definition or what? I'm staring myself blind just trying to understand where that damn None originates from!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 925
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Class interaction

 
0
  #6
Oct 24th, 2008
Ah! Got it. i have been looking at your code and i notice this:
  1. print p.Hi()
the problem is that you dont need to print it becuase the printing is done INSIDE the method, so for this just delete the print statement and just have:
  1. class add:
  2. def __init__(self, name):
  3. self.name = name
  4. def Hi(self):
  5.  
  6. print self.name, "just joined"
  7.  
  8. while True:
  9. i = raw_input("command>")
  10. if i.split()[0] == 'add':
  11. p = add(i.split()[1])
  12.  
  13. p.Hi() #dont print a function that does not return anything
  14.  
  15. else:
  16. print "no"
The only times in which you can PRINT a function is when it returns something, in this case the function p.Hi() didnt return anything so the value None was given.
Hope that helps.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 13
Reputation: Devlan is an unknown quantity at this point 
Solved Threads: 0
Devlan Devlan is offline Offline
Newbie Poster

Re: Class interaction

 
0
  #7
Nov 18th, 2008
I should have posted this a long time ago, but anyway:
Thanks for the help, this cleared up a lot.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC