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

Orri Ganel singingxduck at gmail.com
Tue May 16 23:45:25 CEST 2006


Alan Gauld wrote:

> > 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 at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>
Erm.  Shouldn't that be the following?

class Msg:
   def __init__(self,s):
      self.s = s
   def say(self):
      if *not *self.s: print "No message"
      else: print self.s


-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.



More information about the Tutor mailing list