> How do I create the equivalent of a Java class in Python? I've been looking
> at the reference, and it's been confusing to me at least.
 
Adapted from my book:
 
Java code:
 
class Msg{
   private String txt;
   public Msg(String s){
      this.txt = s;
   }
   public void say(){
      if (this.txt == "")
         System.out.println("No message");
      else
         System.out.println(this.txt);
   }
}
 
Python code:
 
class Msg:
   def __init__(self,s):
      self.s = s
   def say(self):
      if self.s: print "No message"
      else: print self.s
 
Does that help?
There is more on writing classes in the OOP topic of my tutor.
 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to