Commit ae18f76c authored by Gökçe Aydos's avatar Gökçe Aydos 🌈
Browse files

clarifications

parent f7bc096b
Pipeline #13142 passed with stage
in 7 minutes and 55 seconds
......@@ -241,7 +241,7 @@ x.n is C.n # True
##
<!-- the methods must have `self` as an argument -->
Which class method definition does not make sense?
Which class method definition does not make sense? Hint: `self` is just a name.
A) ```py
class Dna:
......@@ -266,7 +266,7 @@ A) I don't know
. . .
Methods typically have a first argument. Every name may be used, but the name `self` seems to be the agreed rule among Python programmers.
Methods *typically* have a first argument. Every name may be used, but the name `self` seems to be the agreed rule among Python programmers.
##
......@@ -300,7 +300,10 @@ but we should not do that and stick to the name `self` that most programmers use
---
But does it make any sense at all to create a function which does not do something particular to the created object? True, so we could also use the class function using the class itself, instead of the instance:
But does it make any sense to create a function which does not do something particular to the created object?
Typically not. In most cases, the methods do something with the created object. Only if we want to keep the function inside the namespace of the class itself, then this makes sense:
```py
class C:
def f():
......@@ -309,8 +312,17 @@ class C:
C.f() # 'hi!'
```
There is actually a use case when we want to keep an object for all the instances of a class: when we want to keep an eye on the number of instances. Refer to ['When can I write a method without self as its argument – Quora](https://www.quora.com/In-python-when-can-I-write-a-method-without-self-as-its-argument-and-where-is-it-used)
<!-- del removes the binding between the name and the object
source:
https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces
<!-- we do not provide the self argument, self points to the new object created by the constructor -->
the statement del x removes the binding of x from the namespace referenced by the local scope. In fact, all operations that introduce new names use the local s
>
<!-- we do not provide the self argument, self (actually the first argument which has been provided (like in the dna example)) points to the new object created by the constructor -->
<!-- a question where the self argument is omitted
class Bot:
......@@ -342,5 +354,3 @@ x.greet() # 'hi!'
<!-- __init__ -->
<!-- how do we change the attributes -->
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment