| | |
Class interaction
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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!
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!
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:
If thats not it could you just maybe clarify it for us.
Thanks
Well a way to do that would be to split the string and do some if statements such as:
python Syntax (Toggle Plain Text)
i = raw_input() if i.split()[0] == 'add': p = add(i) p.hi
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
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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?
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 testingWell, 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?
•
•
•
•
get <__main__.add instance at 0x00A98FD0>python Syntax (Toggle Plain Text)
print p.Hi #This prints the location...
Um... why?

python Syntax (Toggle Plain Text)
print p.Hi() #This prints the location...
•
•
Join Date: Oct 2008
Posts: 13
Reputation:
Solved Threads: 0
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!
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!
Ah! Got it. i have been looking at your code and i notice this:
the problem is that you dont need to
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.
python Syntax (Toggle Plain Text)
print p.Hi()
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)
class add: def __init__(self, name): self.name = name def Hi(self): print self.name, "just joined" while True: i = raw_input("command>") if i.split()[0] == 'add': p = add(i.split()[1]) p.Hi() #dont print a function that does not return anything else: print "no"
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
Check out my Site | and join us on IRC | Python Specific IRC
![]() |
Similar Threads
- how to remove space from string? (PHP)
- Need java tutor? (Java)
- class diagram/uml (Java)
- re: Trouble with GUI (Java)
- Authenticator (Java)
- a Java GUI based program using Swing classes, Vat (Java)
- Sorting Strings (Java)
- Forum lurkers, introduce yourself ... !! (Community Introductions)
- Class that represents sets of integers (C++)
Other Threads in the Python Forum
- Previous Thread: (HELP) wxPython: Update other panels from event triggered in another panel
- Next Thread: How to handle null Resultset(zero rows)
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






