title: Overloading constructors in Python
body: <p>Simply, what is the cleanest or most commonly used way in which <strong>constructors</strong> are <strong>overloaded</strong> in Python?</p>
  <p>I have experience in C# and am new to Python, so I'm still learning what is best practice in Python.</p>
  <p>C# has a very clean way of overloading constructors, so that I can handle different data types being passed as arguments on initialization. I have seen a lot of different answers in how similar results can be achieved in Python, and I don't <strong>NOT</strong> want default parameters or logic within my constructor to handle different data types!!! And I can not understand why people are recommending this as an alternative.</p>
  <p>Here is some C# code, and I basically want to do the same in Python.</p>
  <pre class="lang-cpp prettyprint-override"><code>class SomeClass{
  
      int a;
      double b;
      string c;
  
      public SomeClass(int value1, double value2)
      {
          a = value1
          b = value2
      }
  
      public SomeClass(int value1, string value2)
      {
          a = value1
          c = string
      }
  }
  </code></pre>
  <p>The above code is just an example. I know there are hacks, like testing argument type and adding this logic to my constructor or accepting that Python is not strongly typed and writing logic in my class methods for how value2 may be handled for different types.</p>
  <p>However, the code I am writing is not as simple as the above. I want to handle how arguments of different objects or data types are handled. I would like to overload my constructor for handling objects of different classes and data type i.e. I could pass an object of MyObject&lt;MyObjectClass&gt; or a JSON file or text file/<a href="https://en.wikipedia.org/wiki/Comma-separated_values" rel="nofollow noreferrer">CSV</a> file, etc.</p>
  <p>And I do not want a hacky <em>if</em> statement to test if a JSON file is passed. This is because potentially my constructor for passing a CSV file will contain further arguments and/or different arguments from passing a JSON file.</p>
  <p>I find the way in which C# handles overloading constructors much cleaner as I can easily add further arguments to my JSON file constructor in the future and I only need to fiddle with the one constructor!</p>
  <p>I'm sure Python has a very clean way of achieving the same...?</p>
  <p>It just hit me that overloading is probably non-existent in Python as it is not statically typed. And therefore I am assuming class methods are best suited for my purpose._</p>
  <pre><code>class SomeClass(object)
  
      @classmethod
      def fromtextfilename(cls, textfile):
          # Logic here
          return cls(data)
  
      @classmethod
      def fromtextfilename(cls, jsonfile):
          # Logic here
          return cls(data)
  
      __int__(self,data)
          # Use data to populate properties
  </code></pre>
  <hr/>