c# - class vs static methods -
i'm new in python (come c#), trying figure out how oop works here. started begining try implement vector class. want have basis vectors (i, j, k) defined in vectorclass. in c#, can that:
public class vector { // fields... public vector(int[] array){ //... } public static vector i(){ return new vector(new int[1, 0, 0]); } } exploring python found 2 ways how implement this: using either @classmethod or @staticmethod:
class vector: def __init__(array): #... @classmethod def i(self): return vector([1, 0, 0]) since don't need have access information inside class, should use @classmethod?
i think you've confused naming of arguments bit. first argument class method receives class itself, vector, now, until have subclass. class method implemented this:
@classmethod def i(cls): return cls([1, 0, 0]) generally, instance method (no decorator) calls first argument self, instance. class method has cls, class, can used construct instance. static method takes no "extra" argument, other option, if want return vector, is:
@staticmethod def i(): return vector([1, 0, 0])
Comments
Post a Comment