Skip to content

Commit 1e1a642

Browse files
authored
Merge pull request wilfredinni#334 from classic-arnold/patch-1
Update classmethod.md
2 parents 8c59d55 + da17576 commit 1e1a642

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

docs/builtin/classmethod.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ Python classmethod() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

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+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy