Sharing a piece of code with methods inside a class in Python -


i started making draft 1 of classes supposed used in programm , first wrote piece of code:

import math import numpy np  r = 6.371e6  phi_src, theta_src = 10, 40 phi_det,theta_det = -21, 10  depth_src, depth_det = 0,0 # both on surface  l = 0  class trajectory:      def __init__(self,                  phi_src,                  theta_src,                  phi_det,                  theta_det,                  depth_src,                  depth_det,                  l):          self.phi_src = phi_src         self.theta_src = theta_src         self.phi_det = phi_det         self.theta_det = theta_det         self.depth_src = depth_src         self.depth_det = depth_det         self.l = l     @property      def r(self):         r_src = r - self.depth_src         r_det = r - self.depth_det          x_src = r_src * math.cos(self.phi_src) * math.cos(self.theta_src)         y_src = r_src * math.cos(self.phi_src) * math.sin(self.theta_src)         z_src = r_src * math.sin(self.phi_src)          x_det = r_det * math.cos(self.phi_det) * math.cos(self.theta_det)         y_det = r_det * math.cos(self.phi_det) * math.sin(self.theta_det)         z_det = r_det * math.sin(self.phi_det)          coord_src = np.array((x_src, y_src, z_src))         coord_det = np.array((x_det, y_det, z_det))          l = np.linalg.norm(coord_src - coord_det)          return math.sqrt(r_src**2 + self.l * (1.0 - l - (r_src - r_det) * (r_src + r_det)/l))      def phi(r):         pass   trajectory = trajectory(phi_src,theta_src,phi_det,theta_det,depth_src,depth_det,l)  print(trajectory.r) 

but realized

        r_src = r - self.depth_src         r_det = r - self.depth_det          x_src = r_src * math.cos(self.phi_src) * math.cos(self.theta_src)         y_src = r_src * math.cos(self.phi_src) * math.sin(self.theta_src)         z_src = r_src * math.sin(self.phi_src)          x_det = r_det * math.cos(self.phi_det) * math.cos(self.theta_det)         y_det = r_det * math.cos(self.phi_det) * math.sin(self.theta_det)         z_det = r_det * math.sin(self.phi_det)          coord_src = np.array((x_src, y_src, z_src))         coord_det = np.array((x_det, y_det, z_det))          l = np.linalg.norm(coord_src - coord_det) 

part common methods of class , hence there's no point in calculating numerous times in every method, piece should shared methods. best way that? have put __init__ method? i've heard it's not practice make calculations inside __init__ method.

the common way of declaring function in class not depend on state of object use @staticmethod decorator, followed function definition. pass function parameters.

if need use class level parameters, use @classmethod instead , note pass cls instead of self function (one use variable, doesn't matter. point accessing class attributes , methods instead of of object).

class trajectory:     c = 10  # <<< class level property.      def __init__(self):         self.c = 5  # <<< object level property.      @staticmethod     def foo(a, b):         return * b      @classmethod     def bar(cls, a, b):         return cls.foo(a, b) * cls.c  # <<< references class level method , property.      def baz(self, a, b):         return self.foo(a, b) * self.c  # <<< references object level method , property.  t = trajectory()  >>> t.foo(3, 5) 15  >>> t.bar(3, 5) 150  >>> t.baz(3, 5) 75 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -