943,574 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 772
  • Python RSS
Oct 23rd, 2008
0

Class interaction

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 23rd, 2008
0

Re: Class interaction

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Oct 23rd, 2008
0

Re: Class interaction

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 23rd, 2008
0

Re: Class interaction

Click to Expand / Collapse  Quote originally posted by Devlan ...
python Syntax (Toggle Plain Text)
  1. print p.Hi #This prints the location...
get <__main__.add instance at 0x00A98FD0>
Um... why?
Because you asked for it

python Syntax (Toggle Plain Text)
  1. print p.Hi() #This prints the location...
HTH
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 23rd, 2008
0

Re: Class interaction

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008
Oct 24th, 2008
0

Re: Class interaction

Ah! Got it. i have been looking at your code and i notice this:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Nov 18th, 2008
0

Re: Class interaction

I should have posted this a long time ago, but anyway:
Thanks for the help, this cleared up a lot.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Devlan is offline Offline
13 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: (HELP) wxPython: Update other panels from event triggered in another panel
Next Thread in Python Forum Timeline: How to handle null Resultset(zero rows)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC