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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -