ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> Jython-users
Jython-users
RE: [Jython-users] Why is self used so much
by O'Brien-Strain, Eamonn other posts by this author
Feb 28 2002 7:06PM messages near this date
RE: [Jython-users] Example of Java class reloading | [Jython-users] Trouble with getting to EJB
------_=_NextPart_001_01C1C08B.02BF1970
Content-Type: text/plain;
	charset="iso-8859-1"

The variable "self" is the Python equivalent to Java's "this".
 
The main differences are:

*	 In Python you explicitly pass in the "self" parameter to every
(non-static) method, while in Java the "this" value is automatically
available in the methods.  (You can think of "this" as being an extra
parameter that is invisibly passed in to each method.)
*	In a Java (non-static) method you can refer to an attribute foo
equivalently as either "this.foo" or "foo".  In Python however you have to
use "self.foo".
*	In Java the name "this" is fixed by the language.  In Python the
name "self" is just a programmer convention -- you could use any other name
for the first parameter of methods (but it is not advised because doing so
would confuse other Python programmers)

__ 
Eamonn O'Brien-Strain 
HP Labs 
eob@[...].com 

-----Original Message-----
From: Martin [mailto:martinb@[...].ie]
Sent: Wednesday, February 27, 2002 9:59 AM
To: jython-users@[...].net
Subject: [Jython-users] Why is self used so much


Hi,
Im new to jython and python but have been programming in java for a while. I
was just wondering why it seems that every variable used has to have self
prefixed to it. A basic applet example of mine is below. This wouldnt
compile/run until self was placed before each variable and between the
brackets of init.
I'd just like to know why this is needed or if im doing something wrong.
 
from javax.swing import *
from java.awt import *
 
 
class Button1(JApplet):
 b1 = JButton("Button 1")
 b2 = JButton("Butotn 2")
 def init(self):
  self.cp = self.getContentPane()
  self.cp.setLayout(FlowLayout())
  self.cp.add(self.b1)


------_=_NextPart_001_01C1C08B.02BF1970
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> <HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> 


<META content="MSHTML 5.00.3207.2500" name=GENERATOR> </HEAD>
<BODY> 
<DIV> <FONT color=#0000ff face=Arial size=2><SPAN class=954145418-28022002>The 
variable "self" is the Python equivalent to Java's "this".</SPAN> </FONT></DIV>
<DIV> <FONT color=#0000ff face=Arial size=2><SPAN 
class=954145418-28022002> </SPAN></FONT>&nbsp;</DIV>
<DIV> <FONT color=#0000ff face=Arial size=2><SPAN class=954145418-28022002>The 
main differences are:</SPAN> </FONT></DIV>
<UL> 
  <LI> <FONT color=#0000ff face=Arial size=2><SPAN 
  class=954145418-28022002> &nbsp;In Python you explicitly pass in the "self" 
  parameter&nbsp;to every (non-static) method, while in Java the "this" value is 
  automatically available in the methods.&nbsp; (You can think of "this" as 
  being an extra parameter that is invisibly passed in to each 
  method.)</SPAN> </FONT></LI>
  <LI> <FONT color=#0000ff face=Arial size=2><SPAN class=954145418-28022002>In a 
  Java (non-static) method you can refer to an attribute foo equivalently as 
  either "this.foo" or "foo".&nbsp; In Python however you have to use 
  "self.foo".</SPAN> </FONT></LI>
  <LI> <FONT color=#0000ff face=Arial size=2><SPAN class=954145418-28022002>In 
  Java the name "this" is fixed by the language.&nbsp; In Python the name "self" 
  is just a programmer convention -- you could use any other name for the first 
  parameter of methods (but it is not advised because doing so would confuse 
  other Python programmers)</SPAN> </FONT></LI></UL>
<DIV> <FONT color=#0000ff face=Arial size=2><SPAN class=954145418-28022002>
<P> <FONT face="Courier New" size=2>__</FONT> <BR><FONT face="Courier New" 
size=2> Eamonn O'Brien-Strain</FONT> <BR><FONT face="Courier New" size=2>HP 
Labs</FONT>  <BR><FONT face="Courier New" size=2>eob@[...].com</FONT> 
</P> </SPAN></FONT></DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5p
x"> 
  <DIV align=left class=OutlookMessageHeader dir=ltr> <FONT face=Tahoma 
  size=2> -----Original Message-----<BR><B>From:</B> Martin 
  [mailto:martinb@[...].ie]<BR> <B>Sent:</B> Wednesday, February 27, 2002 9:59 
  AM<BR> <B>To:</B> jython-users@[...].net<BR><B>Subject:</B> 
  [Jython-users] Why is self used so much<BR> <BR></DIV></FONT>
  <DIV> <FONT face=Arial size=2><SPAN 
  class=960425417-27022002> Hi,</SPAN></FONT></DIV>
  <DIV> <FONT face=Arial size=2><SPAN class=960425417-27022002>Im new to jython 
  and python but have been programming in java for a while. I was just wondering 
  why it seems that every variable used has to have self prefixed to it. A basic 
  applet example of mine is below. This wouldnt compile/run until self was 
  placed before each variable and between the brackets of 
  init.</SPAN> </FONT></DIV>
  <DIV> <FONT face=Arial size=2><SPAN class=960425417-27022002>I'd just like to 
  know why this is needed or if im doing something wrong.</SPAN> </FONT></DIV>
  <DIV> <FONT face=Arial size=2><SPAN 
  class=960425417-27022002> </SPAN></FONT>&nbsp;</DIV>
  <DIV> <FONT face=Arial size=2><SPAN class=960425417-27022002>from javax.swing 
  import *<BR> from java.awt import *</SPAN></FONT></DIV>
  <DIV> &nbsp;</DIV>
  <DIV> <FONT face=Arial size=2><SPAN class=960425417-27022002>&nbsp;<BR>class 
  Button1(JApplet):<BR> &nbsp;b1 = JButton("Button 1")<BR>&nbsp;b2 = 
  JButton("Butotn 2")<BR> &nbsp;def init(self):<BR>&nbsp; self.cp = 
  self.getContentPane()<BR> &nbsp; self.cp.setLayout(FlowLayout())<BR>&nbsp; 
  self.cp.add(self.b1)</SPAN> </FONT></DIV></BLOCKQUOTE></BODY></HTML>

------_=_NextPart_001_01C1C08B.02BF1970--

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved