#๐ after learning decorators i reached there
114 messages ยท Page 1 of 1 (latest)
@tall ether
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what specific thing are you asking about?
what is cache and why he wrote self._area = None in the first part
Do you understand what this class is being used for?
to find area of the circle?
Well yes
but when it is constructed, it is only given a radius
so I can make Circle(5)
the area is not calculated until it is asked for though
the instance also has a property setter to change the radius
but it is in the init
and how to call it
I'm not sure what you're asking
how to call init attribute who has no parameter
it's still just an attribute
what is the use of it
class Car:
def __init__(self, color):
self.color = color
self.fuel = 0
in this example, all my car instances start with 0 fuel
I don't need a parameter to tell me that
and it's not something I want to be controlled when the instance is created (for this example)
parameters are just the attributes that we want to be determined by the constructor
ok but it can be modified in runtime?
okay
so in your circle example, the area is only first calculated when asked for
if it's the first time we ask for area, it has to be calculated
if we ask for area more than once, then we just use the previously calculated value (no point in calculating it more than once since it will be the same result)
didn't get it
if I ask you "what is the area of a circle with radius 5", what would you say to me?
i have radius of 5
no, I'm asking you for the area
none
hey wizard, what is the area of a circle with the radius 5?
do i calculate?
yes
78.57
man!!..is it still wrong
no, it is correct
but, I'm asking you the same question two times in a row
which means you shouldn't need to calculate it again
78.57
yes exactly ๐
instead of doing the calculation again, you can just say "oh, this is the same question as last time, I already have the answer"
that's what is happening here
"if we haven't calculated the area yet, then we should calculate it now"
ohkk
and that's why we need this. If we change the radius of the circle, we have to say "our previous area calculation is no longer correct"
yes i am
what is the area of a circle with the radius 5?
78.57
hey wizard, what is the area of a circle with radius 8?
wait...is the last method called after init?
no, only init is called when the instance is created
and property returns getter only?
you should really try writing out the code and running it
you should pretty much never learn new code without also writing it and running it
okay..give me time..i am going to..
ok, so it will always return the area none when we initialise or put a new radius..right? @heavy silo
it's not returning anything yet, it's just storing a placeholder value until the first area is actually calculated
c = Circle(5)
print(c.area)
you would use it like this
yes ok
import math
class Circle:
def __init__(self, r):
self._r = r
self._area = None
@property
def radius(self):
return self._r
@radius.setter
def radius(self, r):
if r < 0:
raise ValurError('Radius must be a non negative')
self._r = r
self._area = None
@property
def area(self):
if self._area is None:
self._area = math.pi * (self.radius ** 2)
return self._area```
ok, I've added a print to the area property so it will display when it has to recalculate the area
!e
import math
class Circle:
def __init__(self, r):
self._r = r
self._area = None
@property
def radius(self):
return self._r
@radius.setter
def radius(self, r):
if r < 0:
raise ValurError('Radius must be a non negative')
self._r = r
self._area = None
@property
def area(self):
if self._area is None:
print("CALCULATING...")
self._area = math.pi * (self.radius ** 2)
return self._area
c = Circle(5)
print(c.area)
print(c.area)
print(c.area)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | CALCULATING...
002 | 78.53981633974483
003 | 78.53981633974483
004 | 78.53981633974483
See how it only says CALCULATING once, even though I've asked for the area 3 times?
why
.
.
make sure you read everything I say carefully
this is exactly what your class is doing by the way. The answer has previously been answered, so it's pointing at the same answer instead of calculating it again
ah..so it is saving in the self._area attribute instead of none
yes, so it can look it up again
oh ok
if the radius changes, we're discarding the old area value since it is no longer correct
!e
import math
class Circle:
def __init__(self, r):
self._r = r
self._area = None
@property
def radius(self):
return self._r
@radius.setter
def radius(self, r):
if r < 0:
raise ValurError('Radius must be a non negative')
self._r = r
self._area = None
@property
def area(self):
if self._area is None:
print("CALCULATING...")
self._area = math.pi * (self.radius ** 2)
return self._area
c = Circle(5)
print(c.area)
print(c.area)
print(c.area)
c.radius = 10
print(c.area)
print(c.area)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | CALCULATING...
002 | 78.53981633974483
003 | 78.53981633974483
004 | 78.53981633974483
005 | CALCULATING...
006 | 314.1592653589793
007 | 314.1592653589793
here I've changed the radius to 10 so it has to recalculate
aif radius changes, the area comes back to none ,, right
yes
np ๐
you should try and write this class again by yourself
open a new file and try
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.