[Tutor] Concept related to python classes
Manprit Singh
manpritsinghece at gmail.com
Mon Sep 7 12:47:01 EDT 2020
Dear sir,
There is a minor modification in the area of the triangle program , using
property, kindly have a look :
class Triangle:
def __init__(self):
self._a = None
self._b = None
self._c = None
self._area = None
@property
def a(self):
"""I'm the 'a' property."""
return self._a
@a.setter
def a(self, a1):
self._a = a1
@property
def b(self):
"""I'm the 'b' property."""
return self._b
@b.setter
def b(self, b1):
self._b = b1
@property
def c(self):
"""I'm the 'c' property."""
return self._c
@c.setter
def c(self, c1):
self._c = c1
@property
def area(self):
s = (self._a + self._b + self._c) / 2
self._area = (s * (s - self._a) * (s - self._b) * (s -
self._c))**0.5
return self._area
tri = Triangle()
tri.a = 3
tri.b = 4
tri.c = 5
print("Area of triangle with sides",tri.a,"&",tri.b,"&",tri.c,"is",tri.area)
tri.a = 4
tri.b = 4
tri.c = 3
print("Area of triangle with sides",tri.a,"&",tri.b,"&",tri.c,"is",tri.area)
The output of this program is
Area of triangle with sides 3 & 4 & 5 is 6.0
Area of triangle with sides 4 & 4 & 3 is 5.562148865321747
Which is desired output.
Need your comments on this program.
Regards
Manprit Singh
On Mon, Sep 7, 2020 at 9:45 PM Manprit Singh <manpritsinghece at gmail.com>
wrote:
> Dear Sir,
> This is again a continuation mail . I have tried to solve the same problem
> of finding area of a triangle using decorators . and it seems more
> effective in comparison to my previous code, which is given below :
>
> class Triangle:
> def __init__(self):
> self._a = None
> self._b = None
> self._c = None
> self._area = None
>
> @property
> def a(self):
> """I'm the 'a' property."""
> return self._a
>
> @a.setter
> def a(self, a1):
> self._a = a1
>
> @property
> def b(self):
> """I'm the 'b' property."""
> return self._b
>
> @b.setter
> def b(self, b1):
> self._b = b1
>
> @property
> def c(self):
> """I'm the 'c' property."""
> return self._c
>
> @c.setter
> def c(self, c1):
> self._c = c1
>
>
> def calcarea(self):
> s = (self._a + self._b + self._c) / 2
> self._area = (s * (s - self._a) * (s - self._b) * (s -
> self._c))**0.5
>
> @property
> def area(self):
> self.calcarea()
> return self._area
>
>
>
> tri = Triangle()
> tri.a = 3
> tri.b = 4
> tri.c = 5
> print("Area of triangle with
> sides",tri.a,"&",tri.b,"&",tri.c,"is",tri.area)
> tri.a = 4
> tri.b = 4
> tri.c = 3
> print("Area of triangle with
> sides",tri.a,"&",tri.b,"&",tri.c,"is",tri.area)
>
> Output is as follows:
>
> Area of triangle with sides 3 & 4 & 5 is 6.0
> Area of triangle with sides 4 & 4 & 3 is 5.562148865321747
>
> Just see, after making the object of the class Triangle and assigning it
> to a variable tri , i am just going in an easy way similar to assigning
> values to the instance variables, although here in this example , by
> writing tri.a, tri.b & tri.c for setting, the setter function for a
> particular instance variable is accessed due to property.
> and immediately after it , writing tri.area gives the area This seems more
> pythonic.
>
> What about adopting this practise ?
>
> Regards
> Manprit Singh
>
>
> On Mon, Sep 7, 2020 at 6:09 PM Alan Gauld via Tutor <tutor at python.org>
> wrote:
>
>> On 07/09/2020 10:59, Manprit Singh wrote:
>>
>> > class Triangle:
>> > def __init__(self, a, b, c):
>> > self.a = a
>> > self.b = b
>> > self.c = c
>> >
>> > def area(self):
>> > s = (self.a + self.b + self.c) / 2
>> > return (s * (s - self.a) * (s - self.b) * (s - self.c))**0.5
>> >
>> > def resize(self, a1, b1, c1):
>> > self.a = a1
>> > self.b = b1
>> > self.c = c1
>>
>>
>> Yes, that looks more like what I'd expect to see.
>>
>> BTW Thanks for posting the area equation, I'd never come across
>> Herons formula before and had to do some reading on Wikipedia :-)
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
More information about the Tutor
mailing list