1

I am reading through Python and came across various ways to somehow perform overloading in Python(most of them suggested use of @classmethod). But I am trying to do something like this as shown in below code. I have kept all the parameters required in the init method itself. What all possible problems may arise from my choice of overloading?

class Vehicle(object):
    def __init__(self, wheels=None, engine=None, model=None):
        print("A vehicle is created")
        self.w = wheels
        self.e = engine
        self.m = model

Now I can create any number of Vehicle objects with different parameters each time. I can say something like:

v = Vehicle(engine=2, wheels='Petrol')
v2 = Vehicle(4, 'Diesel', 'Honda')

or even

v3 = Vehicle()

And later I can say something like v3.w = 10 #for truck and it still works.

So my question is: Is it correct way of overloading apart from @classmethod? What difficulties can I run in later down the path if I use this kind of code?

saurav
  • 427
  • 4
  • 14
  • Usually you do not write class methods, unless something really should be defined at *class* level: this means that the exact instance does not matter, only the behavior of the class should be altered. – Willem Van Onsem Sep 28 '17 at 15:43
  • 1
    You might take a look at https://stackoverflow.com/questions/682504/what-is-a-clean-pythonic-way-to-have-multiple-constructors-in-python – sfjac Sep 28 '17 at 15:45
  • @WillemVanOnsem So my way of "overloading" is good to go? – saurav Sep 28 '17 at 15:45
  • But `@classmethod` and *overloading* are two (almost) orthogonal concepts. – Willem Van Onsem Sep 28 '17 at 15:45
  • 3
    I think he's referring to using `@classmethod`s for factory functions; e.g. https://stackoverflow.com/a/141777/2587908 – sfjac Sep 28 '17 at 15:46
  • https://stackoverflow.com/a/141777/2350068 @WillemVanOnsem – saurav Sep 28 '17 at 15:47
  • The code looks fine. Trying to do anything with `@classmethod` to get something which is in some ways functionally equivalent would be much less readable and hence unpythonic. – John Coleman Sep 28 '17 at 15:52
  • @JohnColeman Thank you sir, I just wanted to know if my code will work fine. – saurav Sep 28 '17 at 15:56

1 Answers1

0

I just went though this same problem and looking into the documentation on Python 3.6 @classmethod is a decorator that is actually short hand for some deeper programming concepts. For anyone like me whose just trying to unpack what python is doing here, in C# or Java I would explain @classmethod as a function that creates a delegate typed to a class, points the delegate at such a classes constructor/method, returns that constructor/method, and allows the returned constructor/method to be used in whatever you define below @classmethod. So essentially, @classmethod is really a syntactical shortcut that does a lot of things.

What OP is doing here is using this syntactic shortcut to create a "factory" which is a very common way of creating instances in many different languages.

I do think its important however to realize that unlike other simple things that you might do in python, there is a lot going on under the hood here. While it's not wrong, it might be more efficient to create a simple factory depending on what you want to get out of it.

If you don't have a back ground in any other languages, I could try to simplify the answer by saying that @classmethod it returns a function to the function that you define below it.

Here's the documentation on Python 3.6. Scroll down to "decorators" to see what it says.

https://docs.python.org/3/glossary.html#term-decorator

Jamie Marshall
  • 1,885
  • 3
  • 27
  • 50