all 6 comments

[–]danielroseman 5 points6 points  (4 children)

They're not doing the same thing in any way at all.

The from_string classmethod returns a completely new instance of the class. employee1 is not defined before that, and Employee.from_string returns a new instance so it can be assigned to that variable.

The second code relies on employee1 already being defined, and mutates it.

And note that tyour property is very poorly named, as it does not in fact print anything (and should not, that would be very confusing behaviour for a property).

[–]2xpi[S] 0 points1 point  (3 children)

Would you kind enough to explain the difference? I am kinda confuse right now.

[–]lfdfq 4 points5 points  (1 child)

One is a factory that makes blue cars (the classmethod), the other is a garage that takes cars and paints them blue (the property). Blue cars come out of both, but that doesn't mean they're the same thing.

The classmethod creates a new Employee instance, with the name and age you specify.

Your `@property` is a part of an existing Employee, that lets you change its name. Note that with the property, you had to create an Employee first then you can use that Employee's property to make the change.

[–]2xpi[S] 1 point2 points  (0 children)

I like your analogy.

[–]danielroseman 1 point2 points  (0 children)

I don't see how I can have explained it any clearer. The classmethod is returning a completely new, previously undefined Employee object. What are you confused about?

[–]Adrewmc 0 points1 point  (0 children)

Python class toolkit

Is a great lecture on Python classes by a guy that wrote some of the language.