
-----------------------------------
Kenster102.5
Mon Nov 02, 2009 10:21 am

Methods Java to Python help?
-----------------------------------
I have recently gotten started into Python, and am still very new at it, but I already have programing knowledge in Java and Turing. 
My question is I have this Java program using Instance and Constructor methods, and am trying to re-write it in Python, but I really can't figure out what I am doing wrong. 

Java Code:
 
import java.awt.*;
public class Fraction
{
  
//Instance Methods learned October 14.
  
//Creating Objects learned October 7.
  public static void main(String


Python Code:
import math
def MainFraction():
    f = Fraction()
    f.num = 2
    f.den = 3
    s = f.size()
    print (f.num + f.den)
    println (s)

class Fraction:
    def __init__(self):
        num
        den

    def size():
        return math.fabs(num/den)

So what must I do differently?
Thanks
Ken

-----------------------------------
wtd
Mon Nov 02, 2009 5:40 pm

RE:Methods Java to Python help?
-----------------------------------
It's worth noting that nowhere do you set up the num and den instance variables.

-----------------------------------
Kenster102.5
Tue Nov 03, 2009 7:13 am

Re: RE:Methods Java to Python help?
-----------------------------------
It's worth noting that nowhere do you set up the num and den instance variables.

Ah, but sir they are stated as instance fields for the class Fraction.

-----------------------------------
wtd
Tue Nov 03, 2009 11:48 am

RE:Methods Java to Python help?
-----------------------------------
[code]class Foo(object):
    def __init__(self, a):
        self.a = a

b = Foo(42)

print b.a[/code]

-----------------------------------
Kenster102.5
Wed Nov 04, 2009 10:03 am

Re: RE:Methods Java to Python help?
-----------------------------------


Okay? Do I always have to do self.a = a or self.num = num for instance methods in Python, I can't just do num = num or num? 

My second question:
 import math
class MainFraction():
    f = Fraction(2,3)
    print (f.num)
    print (f.den)
    s = Size()
    print (s)

class Fraction(object):
    def __init__(self,num,den):
       self.num = num
       self.den = den

class Size(num,den):
      return math.fabs(num/den)

So what do I need to do for the size method to work with the main method? Since it says that the return is outside of the method, also does Python cast automatically?
Ken

-----------------------------------
wtd
Wed Nov 04, 2009 10:24 am

RE:Methods Java to Python help?
-----------------------------------
Have you read any Python tutorials?

-----------------------------------
[Gandalf]
Wed Nov 04, 2009 10:42 pm

RE:Methods Java to Python help?
-----------------------------------
Indeed, it seems like you're a Java coder who just picked up the Python library documentation and started piecing together code.  I could be wrong. :)

Anyway, you'll definitely want to look into a step-by-step guide on OOP in Python.

Also, Python does not "cast automatically", take for instance:
[code]foo = 5 + "2"[/code]
Is foo a string or an integer?  Is it neither?

-----------------------------------
Kenster102.5
Thu Nov 05, 2009 11:04 am

Re: RE:Methods Java to Python help?
-----------------------------------
[quote="[Gandalf] @ Wed Nov 04, 2009 10:42 pm"]Indeed, it seems like you're a Java coder who just picked up the Python library documentation and started piecing together code.  I could be wrong. :)

Anyway, you'll definitely want to look into a step-by-step guide on OOP in Python.

Also, Python does not "cast automatically", take for instance:
[code]foo = 5 + "2"[/code]
Is foo a string or an integer?  Is it neither?[/quote]

Well yes you are totally right, I am a Java Coder who just picked up Python, because it was introduced unofficially into my class cirriculum by another student in my class. So I feel that I should at least try to explore it. 

So what did I do wrong with calling my Size object?

-----------------------------------
wtd
Thu Nov 05, 2009 12:16 pm

RE:Methods Java to Python help?
-----------------------------------
Is "Size" perhaps meant to be a function?

-----------------------------------
Kenster102.5
Thu Nov 05, 2009 5:58 pm

Re: RE:Methods Java to Python help?
-----------------------------------
Is "Size" perhaps meant to be a function?

Yes it is supposed to be a function, I just noticed that myself. Silly me. I will reply back tomorrow, since I am busy right now.

-----------------------------------
Kenster102.5
Wed Nov 11, 2009 12:35 pm

Re: Methods Java to Python help?
-----------------------------------
I am sorry for not having such a quick reply, but now what should I do that would fit this scenario with the Size method.
