File tree Expand file tree Collapse file tree 1 file changed +24
-3
lines changed Expand file tree Collapse file tree 1 file changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,27 @@ Python classmethod() built-in function
16
16
</base-disclaimer-content >
17
17
</base-disclaimer >
18
18
19
- <!-- remove this tag to start editing this page -->
20
- <empty-section />
21
- <!-- remove this tag to start editing this page -->
19
+ ## Examples
20
+
21
+ ``` python
22
+ class Counter :
23
+ def __init__ (self ):
24
+ self .count = 0
25
+
26
+ def increment (self ):
27
+ self .count += 1
28
+ return self .count
29
+
30
+ @ classmethod # passes the class as the first argument to the method instead of passing the instance
31
+ def get_new_instance (cls ):
32
+ return cls ()
33
+
34
+ if __name__ == " __main__" :
35
+ counter = Counter()
36
+ print (counter.increment())
37
+ # 1
38
+
39
+ counter = counter.get_new_instance()
40
+ print (counter.increment())
41
+ # 1
42
+ ```
You can’t perform that action at this time.
0 commit comments