简体   繁体   English

在实例变量之外使用Class方法/属性

[英]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. vol函数与我在课堂上定义的@property完全相同。 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: 为了使此代码正常工作,我必须复制并粘贴类@property并将其转换为如下所示的常规函数​​:

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? 所以我的问题是,有没有一种方法可以重新设计我的类,以便可以调用在该Spheroid类中定义的@property /方法而不创建实例,从而使Z = vol(X,Y)起作用?

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. 如果您的目标是使vol(x,y)起作用,则可以定义vol以使用传递给它的参数创建一个新对象,调用该属性,然后返回。

or you could make a function on your spheroid class called Vol, and make that function static. 或者您可以在椭球体类上创建一个名为Vol的函数,并使该函数静态化。 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: 我会遵循scott_fakename的意见,并使用这样的静态方法:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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