+ 2
Why should I create classmethod instead of new class?
Why....
8 Antworten
+ 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)
+ 1
I was asking for classmethods... .not regular methods which is far more different ...
+ 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.
+ 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.
0
Anyway there is still not clear answer.... Please give me real life examples....
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.
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?
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.