Below was the post that was sent from the wrong email. Not sure if the first 
post went through, so in the event it did not, I will post again; if it was 
posted twice, I apologize for the redundancy. 

Subject: Re: Help understanding classes

Thank you Alan and Danny. It amazes me at the lengths you guys, as well as 
everyone else who contributes,  will go to to help explain things to us; it is 
greatly appreciated!

Alan, I decided to dumb down the learning classes just a little. By this I 
mean, I am not using Tkinter to learn classes. I am using one of the examples 
from your website, which I did change it just a little. I figured, I am having 
a hard time wrapping my head around classes and Tkinter would just add to the 
confusion. 

So, I have the below code. When I run this from terminal, it obviously prints 
“This is a test.” If I may, break the code down and ask questions as it 
pertains to the code?

#################
class Message:
    def __init__(self, aString):
        self.text = aString

    def printIt(self):
        print self.text

m = Message("This is a test")
m.printIt()

##################

With the first part…
class Message:
    def __init__(self, aString):
        self.text = aString
Will I always use “_init_” when defining the first function in a class? I 
noticed on your website, you created a class where you did not use “_init_” 
(see below). Was this because you did not define a function? 
class BalanceError(Exception): 
      value = "Sorry you only have $%6.2f in your account” 

I noticed that I can change “text” to anything and I still get the same results 
by running the code; I changed them to “blah” just as a test.

When I define a function in a class, will I always use “self” as the first 
entry in the parenthesis? 

On the next part…
m = Message("This is a test")
m.printIt()
I noticed I cannot run “printIt()” unless I make it an object i.e. “m = 
Message("This is a test”)…?” 
I noticed I could change "m = Message("This is a test”)” to "m = 
Message(raw_input()),” which works. 
What if I wanted to create a function in Message that receives text from 
another function and then prints that text instead of the text from  “m = 
Message("This is a test”)…; can I pass or return values to another function 
inside a class? The”self” is really throwing me off, when I think about 
creating different functions that do misc things just to practice. For example, 
I have a function that kills a Linux program. I just don’t see how to rethink 
that function to where it could be defined in a class?  
def kill_proc(process1):
    i = psutil.Popen(["ps", "cax"], stdout=PIPE)
    for proc in psutil.process_iter():
        if proc.name(process1):
            proc.kill()

Would it be something like…?
class processKiller:

    def _init_(self): 

    def kill_proc(self, process1):
        i = psutil.Popen(["ps", "cax"], stdout=PIPE)
        for proc in psutil.process_iter():
            if proc.name(process1):
                proc.kill()
Then outside of the class, call it like so…?
p = processKiller()
p.proc.kill()

Again, I am just practicing, trying to wrap my head around classes and 
understand how to create and use them.

Oh yeah, Alan I preordered your new book maybe a month or so ago. Any word on 
when it will be released and shipped?

Again, thanks. 

Bo 
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to