Original question
I've seen pretty much in all examples people first making an instance of a class:
ss = StandardScaler()
and only after that use the methods from the instance: ss.fit_transform(df)
, rather than calling the method on the class itself: StandardScaler().fit_transform(df)
.
Is this because of:
- There are cases, which would throw an error otherwise.
- There are cases, which don't throw an error, but produce different results (scary!)
- Prevents repetition of code (but it's ok, if its used only once.)
- It's better to do just one thing on one line of code.
- Aesthetics & opinion.
- Some other reason, please let me know!
Some answers thus far
Thank you for the answers that raised many clarifying points, here's some as I understand them. Please correct me if I'm mistaken.
Potential reasons I suggested for making an instance first:
There are cases, which would throw an error otherwise.
- Thomas Weller's answer below states that there shouldn't be, since calling the method on the class creates a temporary instance - it just doesn't get stored in a variable.
There are cases, which don't throw an error, but produce different results (scary!)
- Thomas Weller's answer below states that there shouldn't be, since calling the method on the class creates a temporary instance.
It's ok to call on the class itself, if its used only once.
- This seems to be true, as there is no reason to store the instance in a variable and repetition is not a problem.
It's better to do just one thing on one line of code.
- Readability is more important than doing just one thing per line. In my opinion, both versions are just as clear and readable.
Aesthetics & opinion
- There's some of these involved as well.
Some other reason, please let me know!
- Of course object oriented programming is useful in many ways, but my question concerned only the isolated use of a class and a method someone else has already programmed for me.
- My question wasn't concerned whether or not you can put parameters inside the class or the method - my example actually does this:
np.random.default_rng(0).integers(10, size=(4,5))
Code Example
import numpy as np
from sklearn.preprocessing import StandardScaler
# Here I'm using .interegs() without making an instance first
int_array1 = np.random.default_rng(0).integers(10, size=(4,5))
# Here I'm using .interegs() without making an instance first
int_array2 = StandardScaler().fit_transform(int_array1)
# This time instantiating before using for comparison
rng = np.random.default_rng(0)
int_array3 = rng.integers(10, size=(4,5))
ss = StandardScaler()
int_array4 = ss.fit_transform(int_array3)
print(int_array1)
print(int_array2)
print(int_array3)
print(int_array4)
Output has the same results regardless of instantiation.
[[8 6 5 2 3]
[0 0 0 1 8]
[6 9 5 6 9]
[7 6 5 5 9]]
[[ 0.88354126 0.22941573 0.57735027 -0.72760688 -1.70856429]
[-1.68676059 -1.60591014 -1.73205081 -1.21267813 0.30151134]
[ 0.2409658 1.14707867 0.57735027 1.21267813 0.70352647]
[ 0.56225353 0.22941573 0.57735027 0.72760688 0.70352647]]
[[8 6 5 2 3]
[0 0 0 1 8]
[6 9 5 6 9]
[7 6 5 5 9]]
[[ 0.88354126 0.22941573 0.57735027 -0.72760688 -1.70856429]
[-1.68676059 -1.60591014 -1.73205081 -1.21267813 0.30151134]
[ 0.2409658 1.14707867 0.57735027 1.21267813 0.70352647]
[ 0.56225353 0.22941573 0.57735027 0.72760688 0.70352647]]