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!

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:

i = raw_input()
if i.split()[0] == 'add':
    p = add(i)
    p.hi

If thats not it could you just maybe clarify it for us.
Thanks

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?

print p.Hi                     #This prints the location...

get <__main__.add instance at 0x00A98FD0>
Um... why?

Because you asked for it ;)

print p.Hi()                     #This prints the location...

HTH

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!

Ah! Got it. i have been looking at your code and i notice this:

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:

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"

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.

I should have posted this a long time ago, but anyway:
Thanks for the help, this cleared up a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.