简体   繁体   中英

Using a Class method/property outside of an instance variable

I have a class as follows:

class Spheroid(object):                                                                                                                                                        
  def __init__(self,shortt,longg):                                                                                                                                           
    self.shortax = shortt                                                                                                                                                  
    self.longax  = longg                                                                                                                                                   
    self.alpha=self.longax/self.shortax                                                                                                                                    

  @property                                                                                                                                                                                                                                                                                                                                           
  def volume(self):                                                                                                                                                          
    return (4*np.pi/3) * self.shortax * self.shortax * self.longax

In a piece of code later on, I use a volume function as follows:

x=np.arange(5,8.5,dx)
y=np.arange(5,30,dy)
X,Y = np.meshgrid(x,y)

Z = vol(X,Y)

The vol function is exactly the same as the @property I defined in my class. To get this code to work, I've had to copy and paste the class @property and turn it into a regular function like this:

def vol(a,b):
    return (4*np.pi/3) * a * a * b

I was always told that copying and pasting code is a sign that I'm doing something wrong. So my question is, is there a way I can redesign my class so that I can call the volume @property / method I defined in that Spheroid class without creating an instance, so that the Z = vol(X,Y) would work?

Thanks

if your goal is to make vol(x, y) work, you could define vol to create a new object with the parameters passed to it, call that property, and then return.

or you could make a function on your spheroid class called Vol, and make that function static. Then you could have the instance version of that function just call the static version.

I would follow scott_fakename advise and use a static method like this:

class Spheroid(object):

    def __init__(self, shortt, longg):
        self.shortax = shortt
        self.longax = longg
        self.alpha = self.longax / self.shortax

    @property
    def vol(self):
        return Spheroid.volume(self.shortax, self.longax)

    @staticmethod
    def volume(shortax, longax):
        return (4 * np.pi / 3) * shortax * shortax * longax

# outside the instance call it like this
Spheroid.volume(X, Y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM