Why would I use classmethod constructor in Python? -


i reading effective python slatkin. in item 24, talks achieving polymorphism in python using classmethod functions play role of constructors.

however, not clear me why necessary. why can not achieve same goal using __init__ , overriding in every derived class, same way we're overriding classmethod?

in case, has 1 constructor per class, why not use regular init purpose rather classmethod?

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

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

in examples given in book, classmethod doesn't produce single element. different classes support same classmethod (same signature) produce instances or how many produce, delegated class.

the pathinputdata class, example, produces inputs based on config['data_dir'] configuration, using os.listdir() read input files. can imagine databaseinputdata class provides the same generate_inputs() class method, instead connects database , runs sql query. it'll different configuration. etc.

you can't __init__ method; that's initialising single instance. if there 0 instances produce of class, __init__ wouldn't called, still idea delegate responsibility find out how many instances must produced to class.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -