[Tutor] How do I create the equivalent of a Java class in Python?

Alan Gauld alan.gauld at freenet.co.uk
Sun May 14 16:41:58 CEST 2006


> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060514/bf0c9c3c/attachment.htm 


More information about the Tutor mailing list