开发者

How to use static/helper method in a class?

I'm writting a Fraction class and I am trying to use gcd(a,b) in the initialization of a Fraction object. However, when I was trying to do this it would not work WITHOUT the Fraction part of Fraction.gcd(a,b). I used @staticmethod here, but it does absolutely nothing, i.e. my code works the same without it.

Is there anyway I can call gcd without putting Fraction. in front of it? In Java I would normally create a static method and then just call it. I could very easily put the GCD code inside of the init, but I am trying to learn here!

I am missing a lot here. Can anyone explain: static methods, helper methods in a class and pretty much how I can use various methods inside of a class?

class Fraction(object):
    def __init__(self, a, b):
        if Fraction.gcd(a, b) > 1:
            d = Fraction.gcd(a, b)
  开发者_StackOverflow社区          self.num = a/d
            self.denom = b/d
        else:
            self.num = a
            self.denom = b

    @staticmethod    
    def gcd(a,b): 
        if a > b: a,b = b,a
        while True:
            if b % a == 0: return a
            a, b = b%a, a

    def __repr__(self):
        return str(self.num) + "/" + str(self.denom)


Don't forget, in Python not everything needs to be in a class. There's nothing about gcd that makes it better-suited to being a class method than a standalone function: so take it out of the class. Now you can just call gcd(a, b).


Think of methods in a class just like any other class attribute -- reference them on self:

def __init__(self, a, b):
    if( self.gcd(a,b) > 1):
        d = self.gcd(a,b)

It doesn't matter whether it's an instance method, class method, or static method.

While you certainly can use a staticmethod if you want to keep the code associated with the class, it's usual in Python to use a module-level function, in which case you can call it as gcd:

def gcd(a,b): 
    if a > b: a,b = b,a
    while True:
        if b % a == 0: return a
        a, b = b%a, a

class Fraction(object):
    def __init__(self, a, b):
        if( gcd(a,b) > 1):
            d = gcd(a,b)


If you have a big method within your class that requires many calls to a static method you can define a local function object and assign the method to it so you can call this function instead.

For Static Method gdc:

class Fraction(object):    
    def __init__(self, a, b):
        gcd = Fraction.gcd
        if( gcd(a,b) > 1):
            d = gcd(a,b)
            self.num = a/d
            self.denom = b/d
        else:
            self.num = a
            self.denom = b

    @staticmethod    
    def gcd(a,b): 
        if a > b: a,b = b,a
        while True:
            if b % a == 0: return a
            a, b = b%a, a

    def __repr__(self):
        return str(self.num) + "/" + str(self.denom)

For Instance Method gdc:

class Fraction(object):    
    def __init__(self, a, b):
        gcd = self.gcd
        if( gcd(a,b) > 1):
            d = gcd(a,b)
            self.num = a/d
            self.denom = b/d
        else:
            self.num = a
            self.denom = b

    def gcd(self,a,b): 
        if a > b: a,b = b,a
        while True:
            if b % a == 0: return a
            a, b = b%a, a

    def __repr__(self):
        return str(self.num) + "/" + str(self.denom)

So

gcd = Fraction.gcd

and

gcd = self.gcd

will allow you to call (without Fraction at the beginning as per your request :))

gcd(a,b) 

Also, if you want some basic examples of python classes and instance/static methods have a look at some of my blog posts, specially the one called "Factorial and Fibonacci in Jython":

http://carlosqt.blogspot.com/search/label/Jython


I think you are referring to Java's "import static" feature. Just to clarify: as Java enforces object orientation, it cannot have "modules" like other languages. So using import static Math.*; for example will make all static methods on Math available to be called without the class name. In Python you can just add this function outside a class and call it.


That's how static methods work. You call them via Classname.methodname() (or via instance.methodname(), but self won't be available inside the method).

What you want is a regular function on the module level. Define it outside the class and do not decorate it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜