Why should I create classmethod instead of new class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why should I create classmethod instead of new class?

Why....

10th Nov 2016, 1:24 PM
Minko Terziyski
Minko Terziyski - avatar
8 Answers
+ 3
classes usually represent something like a person who have data that represents it (ID number, name, DOB,....) and the methods are a way to manipulate the data each object holds (the specific data that object holds)
10th Nov 2016, 4:03 PM
Burey
Burey - avatar
+ 1
I was asking for classmethods... .not regular methods which is far more different ...
10th Nov 2016, 7:35 PM
Minko Terziyski
Minko Terziyski - avatar
+ 1
What are "class methods" and "instance methods", in Python? http://softwareengineering.stackexchange.com/a/306095 One of the other answers indicates they're used to "create new instances with alternate constructors and aren't incredibly common." As the question states, this is potentially evidence I don't know Python at all...since I'll be reading that myself.
10th Nov 2016, 7:46 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
So...this is new to me but so far I interpret 'alternate constructor' to mean 'polymorphic class'. Perhaps someone with direct experience will assist while I fumble around the concept. I'll be back if I get something.
10th Nov 2016, 8:11 PM
Kirk Schafer
Kirk Schafer - avatar
0
Anyway there is still not clear answer.... Please give me real life examples....
10th Nov 2016, 7:53 PM
Minko Terziyski
Minko Terziyski - avatar
0
if the new class would have identical attributes and methods to the original, a class method allows you to change the parameters needed to initiate the class without copying/repeating all the code in the original class. the rectangle / square example is good. all the operations on the square and rectangle are identical. the new_square class method allows you to simply enter the side length instead of having to enter it twice when creating an instance of the rectangle class.
31st Dec 2016, 2:33 PM
David Crowley
David Crowley - avatar
0
@David Crowley: wouldn't it be better to have a class Square that inherits from Rectangle with a constructer that just takes one side length and initializes the dimensions?
5th Jan 2017, 9:15 PM
Joshua Smith
Joshua Smith - avatar
0
@Joshua Smith: possibly. or set up the rectangle class so the second side length is an optional argument and let the constructors logically handle squares versus rectangles. This intro course is simply outlining what tools are available. It doesn't delve that deeply into tactics or strategy.
5th Jan 2017, 9:37 PM
David Crowley
David Crowley - avatar