#๐Ÿ”’ after learning decorators i reached there

114 messages ยท Page 1 of 1 (latest)

tall ether
#

I don't why the tutor wrote like this...please help me understand this

little mangoBOT
#

@tall ether

Python help channel opened

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.

heavy silo
tall ether
#

what is cache and why he wrote self._area = None in the first part

heavy silo
#

Do you understand what this class is being used for?

tall ether
#

to find area of the circle?

heavy silo
#

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

tall ether
#

but it is in the init

heavy silo
#

Yes, but it is not a parameter

#

you can have attributes that are not parameters

tall ether
heavy silo
#

I'm not sure what you're asking

tall ether
#

how to call init attribute who has no parameter

heavy silo
#

it's still just an attribute

tall ether
#

what is the use of it

heavy silo
#
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)

heavy silo
tall ether
#

ok but it can be modified in runtime?

heavy silo
#

yes

#

it's just not decided during instantiation

tall ether
#

okay

heavy silo
#

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)

tall ether
#

didn't get it

heavy silo
#

if I ask you "what is the area of a circle with radius 5", what would you say to me?

tall ether
#

i have radius of 5

heavy silo
#

no, I'm asking you for the area

tall ether
#

none

heavy silo
#

I'm asking you, wizard

#

I'm giving you an example

tall ether
#

oh mb

#

pi r square

heavy silo
#

hey wizard, what is the area of a circle with the radius 5?

tall ether
#

do i calculate?

heavy silo
#

yes

tall ether
#

78.57

heavy silo
#

great

#

hey wizard, what is the area of a circle with radius 5?

tall ether
#

man!!..is it still wrong

heavy silo
#

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

tall ether
#

78.57

heavy silo
#

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"

tall ether
#

ohkk

heavy silo
#

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"

tall ether
#

it has to click but it didnt

#

second last part is clear to me btw

heavy silo
#

hey wizard, what is the area of a circle with the radius 5?

#

@tall ether still there?

tall ether
#

yes i am

heavy silo
#

what is the area of a circle with the radius 5?

tall ether
#

78.57

heavy silo
#

hey wizard, what is the area of a circle with radius 8?

tall ether
#

wait...is the last method called after init?

heavy silo
#

no, only init is called when the instance is created

tall ether
#

and property returns getter only?

heavy silo
#

yes, but your radius also has a setter

#

can you paste the code of this class?

tall ether
#

its in my phone

#

its a video ss

heavy silo
#

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

tall ether
#

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

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

tall ether
#

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```
heavy silo
#

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)

little mangoBOT
heavy silo
#

See how it only says CALCULATING once, even though I've asked for the area 3 times?

tall ether
#

why

heavy silo
#

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

tall ether
#

ah..so it is saving in the self._area attribute instead of none

heavy silo
#

yes, so it can look it up again

tall ether
#

oh ok

heavy silo
#

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)

little mangoBOT
heavy silo
#

here I've changed the radius to 10 so it has to recalculate

tall ether
heavy silo
#

yes

tall ether
#

ohkk

#

got it

#

nice..i can move forward now

#

thank you fashoomp

heavy silo
#

np ๐Ÿ™‚

#

you should try and write this class again by yourself

#

open a new file and try

tall ether
#

okay

#

bye

#

i am closing this

#

!close

little mangoBOT
#
Python help channel closed with !close

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.