-3

I am reading Effective Python by Slatkin. In item 24, he talks about achieving polymorphism in python by using classmethod functions that play the role of constructors.

However, it is not clear to me why this is necessary. Why can we not achieve the same goal by simply using __init__ and overriding it in every derived class, the same way we're overriding the classmethod?

In his case, he has only one constructor per class, so why not use regular init for that purpose rather than classmethod?

You can see what's item 24 here, unfortunately details are missing: http://ptgmedia.pearsoncmg.com/images/9780134034287/samplepages/9780134034287.pdf

More details here: http://qiita.com/giwa/items/fd563a93825714cffd70

Baron Yugovich
  • 3,843
  • 12
  • 48
  • 76
  • I'm trying to read this on Google Books: https://books.google.com/books?id=ocmqBgAAQBAJ&pg=PA64&lpg=PA64&dq=Use+@classmethod+Polymorphism+to+Construct+Objects+Generically+slatkin&source=bl&ots=ZCiluYizaC&sig=QfGv_bVYM2dYdNyXvHH-jrWM9C8&hl=en&sa=X&ved=0ahUKEwiR8dy7h_TMAhVD6CYKHXMYDH0Q6AEILjAD#v=onepage&q=Use%20%40classmethod%20Polymorphism%20to%20Construct%20Objects%20Generically%20slatkin&f=false – Martijn Pieters May 25 '16 at 01:23
  • 4
    Still, if you could *summarise* the arguments made by Slatkin **here**, we can keep this question open and I can attempt to answer it. Without those details, the question doesn't stand on its own and can only be closed. – Martijn Pieters May 25 '16 at 01:24
  • And now I've seen what you missed; the class methods don't produce **one** instance, they produce a sequence. – Martijn Pieters May 25 '16 at 01:29
  • I think the sample PDF is auto-generated, the copy I was served does not include item 24. – Martijn Pieters May 25 '16 at 01:30
  • 1
    It's too much typing to include all his code. Still, the point is, the classmethod is used for polymorphism, not for creating a sequence of items. That's just a side note, the main point here is polymorphism. – Baron Yugovich May 25 '16 at 01:32
  • Why isn't that polymorphism? The class is made responsible for figuring out how to create instances for a given configuration. How the configuration is used **differs from class to class**. – Martijn Pieters May 25 '16 at 01:33
  • And you asked why you can't simply use the `__init__` for this. That's because `__init__` is called for *a single instance*, after it was created. So, no, you can't use that to produce 0 or more instances. How would the `__init__` produce multiple items, or even 0? – Martijn Pieters May 25 '16 at 01:49

1 Answers1

3

In the examples given in the book, the classmethod doesn't produce a single element. All different classes support the same classmethod (same signature) but what they do to produce the instances or how many they produce, is delegated to the class.

The PathInputData class, for example, produces inputs based on the config['data_dir'] configuration, using os.listdir() to read the all input files. You can imagine a DatabaseInputData class that provides the same generate_inputs() class method, but instead connects to a database and runs a SQL query. It'll look for different configuration. Etc.

You can't do this with the __init__ method; that's for initialising a single instance. If there are 0 instances to produce of the class, __init__ wouldn't even be called, but it still is a good idea to delegate the responsibility to find out how many instances must be produced to the class.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You can see item 24 here, but not much detail, unfortunately http://ptgmedia.pearsoncmg.com/images/9780134034287/samplepages/9780134034287.pdf – Baron Yugovich May 25 '16 at 01:19
  • You can see details here: http://qiita.com/giwa/items/fd563a93825714cffd70 – Baron Yugovich May 25 '16 at 01:22
  • And here https://books.google.com/books?id=ocmqBgAAQBAJ&pg=PA64&lpg=PA64&dq=effective+python+item+24+use+polymorphism+to+construct+objects+generically&source=bl&ots=ZCiluYiAcz&sig=XBx3spHBqq7mcimUR9f3MVd6ugs&hl=en&sa=X&ved=0ahUKEwjOy5zzh_TMAhUHfVIKHXOWAhcQ6AEIHzAB#v=onepage&q=effective%20python%20item%2024%20use%20polymorphism%20to%20construct%20objects%20generically&f=false – Baron Yugovich May 25 '16 at 01:23