java to python q's

ullrich at math.okstate.edu ullrich at math.okstate.edu
Tue May 16 14:59:42 EDT 2000


In article <001d01bfbe7f$75c78fc0$2801000a at iel.ie>,
  shogan at iel.ie wrote:
>
> (from the java code below)
> 1) whats the differance between public and private and what is the
> equivalent in python?
> 2) how do i declare the variable nvsequence of type NvSequence in
python?

     Not sure whether this is the question you meant to ask.
Variables in Python do not have types or declarations - a
variable springs into existence when you assign something
to it, and you can assign any value whatever to any variable
whatever.

     If NvSequence is a class type and you want to create an
instance of that class you say

nvsequence = NvSequence()

(or "nvsequence = new NvvSequence()" if you're a, um, new guy...
(as it were))

> 3) is this.id = id; the same as self.id = id, in python?

     Yes and no. I gather that "this" is a magic word in Java
referring to the instance on which the method was invoked
(it's "self" where I come from, "me" in other places.) The
instance is _explicitly_ passed to the method in Python -
you can give that parameter any name you like. Ie, the
two snippets

class C:
   def f(self, n)
      self.n = n

and

class C:
   def f(this, n)
      this.n = n

do exactly the same thing. (They both _create_ an instance field,
sorry, instance attribute named "n" if it didn't already exist,
and assign the passed value to it.)

   (If you say self.id = id you're going to be assigning
the built-in function "id" to self.id, btw,)

> public class Message
> {
>  private int id;
>  private int subId;
>  private NvSequence nvSequence;
>
>  file://overloaded constructor 1
>  file://sets up member vars id, subId of type int and nvSequence of
type
> NvSequence
>  public Message(int id, int subId, NvSequence nvSequence)
>  {
>   this.id = id;
>   this.subId = subId;
>
>   this.nvSequence = nvSequence;
>  }
>
> 4)in java:
> public Message(int id, int subId)
>  {
>   this.id = id;
>   this.subId = subId;
>
>   this.nvSequence = null;
>  }
> how do i set this.nvSequence to null in python?
> is it:
>  def msg(id,subid,nvSequence=None)?

    Again, a little puzzled. If you want to use None as
the default value for the nvSequence parameter you
say "def msg(id,subid,nvSequence=None)". But that
does not set anything to anything, that just means
that None is the default, so saying

msg(Id, subid)

is the same as

msg(Id, subid, None).

If you want to set this.nvSequence to null you say
self.nvSequence = None; this is a different question.

     Supposing that you want AClass to have an nvSequence
attribute, and you want the nvSequence attribute of an
instance to be None unless you assign it to something
otherwise, the canonical way to do that is like so:

def AClass:
   nvSequence = None
   def __init__(self, nvSequence = None):
      if nvSequence:
         self,nvSequence = nvSequence

Now AClass has an attribute nvSequence which equals None,
and instances inherit this, unless you pass something
else to the constructor:

aninstance = AClass()
#aninstance.nvSequence == None, inherited from AClass

aninstance = AClass(someNvSequence):
#aninstance.nvSequence == someNvSequence

> 5)how/what modules do i use to find the index of the first/second
integer in
> a string?

    If you want the first and second characters that equal 7
you could use stuff in the string module. If you want the
first and second characters that happen to be digits I
don't think there's a builtin function that does it in
one line - you could do it by hand

def firstdigit(s, start=0):
   for j in range(start, len(s)):
      if s[j] in string.digits:
         return j
      else:
         return -1

def firstandseconddigits(s):
   first = firstdigit(s)
   second = firstdigit(s, first+1)
   return first, second

(Seems like saying

def firstdigit(s, start=0)
   for c in s[start:]:
     if c in digits
        return ???

is more pythitudinous, but if you do that
you need to find the index of c after you
find what c works...)

Or you could use regular expressions.

DU

>
> =====================
> Shaun Hogan
> Interactive Enterprise Ltd.
> alt. E-mail : shaun_hogan at yahoo.com
> The University of Limerick Rollerhockey Club
> URL: http://www.csn.ul.ie/~dek/ulrhc
> Phone: +353 86 8342529
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list