Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Classes
#10
(Feb-15-2019, 04:55 PM)Lonewolf Wrote: can i ask something?
For question 1 my exercise says : Set in all classes the appropriate methods to retrieve and store a new value for each class attribute.
How you do that?

Short answer: in python you don't use setter and getters unless you really have to. And in this case you definitely don't need them.

Long answer: In some programming languages you are required to use methods to retrieve (getter) and to store (setter) for each class attribute. In python you don't unless you have to. Sorry to say that, but actually creating getters and setters for each attribute is considered bad programming in python. Probably your teacher comes from Java or other language that requires them and is following the same pattern with python. Now I don't know if you would want to tell them this but it's true.

Let's create yet another class - Circle, just to use as an example in the following explanations:

class Circle:
    def __init__(self, radius):
        self.radius = radius

# create circle with radius=2
circle = Circle(radius=2)

# print the circle radius
print('radius = {}'.format(circle.radius)) # i.e. here you retrieve the value of attribute radius

# change (set) new radius
circle.radius = 5 # here you set the value

# print the circle radius
print('radius = {}'.format(circle.radius)) # i.e. here you retrieve the value of attribute radius
Output:
radius = 2 radius = 5
as you can see you don't need to have special methods to set and retrieve the value of attribute radius.

What your teacher wants is something like this

class Circle:
    def __init__(self, radius):
        self._radius = radius # we use _radius as an internal "storage" for the value of attribute radius

    def get_radius(self):
        return self._radius

    def set_radius(self, value):
        self._radius = value 

# create circle with radius=2
circle = Circle(radius=3)

# print the circle radius
print('radius = {}'.format(circle.get_radius())) # i.e. here you retrieve the value of attribute radius

# change (set) new radius
circle.set_radius(10) # here you set the value

# print the circle radius
print('radius = {}'.format(circle.get_radius())) # i.e. here you retrieve the value of attribute radius

# However you can still do following and change the radius without using the set method
circle._radius = 5
# print the circle radius
print('radius = {}'.format(circle.get_radius())) # i.e. here you retrieve the value of attribute radius

As I said, this is bad code, so don't do it
. From the last example you can see that you don't need the set_radius method. You can just change the self._radius.

Now what you will do in python if you have to? It may be a bit advanced for you, so don't worry if you don't understand it. The take away is - you do use setters and/or getters only when you really need them.

let's say you want to add another attribute to Circle class - diameter. you know that diameter = radius*2. That means that you need radius and diameter to be always in sync. what you will do?

Also if you want to calculate the area of circle and define attribute area? You need to calculate using circle radius and you should not be able to set the area...

class Circle:
    def __init__(self, radius):
        self.radius = radius

    # this is the "getter" for diameter
    @property
    def diameter(self):
        return self.radius * 2 # return diameter calculated from radius

    #this is the "setter" for diameter
    @diameter.setter
    def diameter(self, value):
        self.radius = value / 2 # here we use setter so that we can change the value of self.radius

    @property
    def area(self):
        return 3.14 * self.radius **2

# create circle with radius=2
circle = Circle(radius=3)

# print the circle radius
print('radius = {}'.format(circle.radius)) # i.e. here you retrieve the value of attribute radius
# print the circle diameter
print('diameter = {}'.format(circle.diameter)) # i.e. here you retrieve the value of attribute diameter

# change (set) new diameter
circle.diameter = 10 # here you set the value
# print the circle radius
print('radius = {}'.format(circle.radius)) # i.e. here you retrieve the value of attribute radius
# print the circle diameter
print('diameter = {}'.format(circle.diameter)) # i.e. here you retrieve the value of attribute diameter

print('area = {}'.format(circle.area)) # i.e. here you retrieve the value of attribute area

circle.area = 100 # here you will get error
Output:
radius = 3 diameter = 6 radius = 5.0 diameter = 10.0 area = 78.5
and then you get this error
Error:
Traceback (most recent call last): File ****, line 36, in <module> circle.area = 100 # here you will get error AttributeError: can't set attribute
As I said - probably you don't understand everything but I just wanted to show how you would properly do it in python.

As to the grid - it's not very clear from your assignment if you need just a list that will hold just 12 shapes, or a grid that will resemble 11x29 grid, i.e. list of lists. In any case it's not what you did. print your grid and see what you egt. Is it really what you want?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Help with Classes - by Lonewolf - Feb-14-2019, 05:13 PM
RE: Help with Classes - by woooee - Feb-14-2019, 08:06 PM
RE: Help with Classes - by Lonewolf - Feb-15-2019, 08:05 AM
RE: Help with Classes - by buran - Feb-15-2019, 09:44 AM
RE: Help with Classes - by Lonewolf - Feb-15-2019, 10:06 AM
RE: Help with Classes - by buran - Feb-15-2019, 10:39 AM
RE: Help with Classes - by Lonewolf - Feb-15-2019, 11:07 AM
RE: Help with Classes - by buran - Feb-15-2019, 11:32 AM
RE: Help with Classes - by Lonewolf - Feb-15-2019, 04:55 PM
RE: Help with Classes - by buran - Feb-15-2019, 07:38 PM
RE: Help with Classes - by Lonewolf - Feb-15-2019, 09:17 PM
RE: Help with Classes - by buran - Feb-15-2019, 09:34 PM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 08:09 AM
RE: Help with Classes - by buran - Feb-16-2019, 08:18 AM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 08:22 AM
RE: Help with Classes - by buran - Feb-16-2019, 08:22 AM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 08:55 AM
RE: Help with Classes - by buran - Feb-16-2019, 08:25 AM
RE: Help with Classes - by buran - Feb-16-2019, 09:02 AM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 09:17 AM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 10:52 AM
RE: Help with Classes - by buran - Feb-16-2019, 11:25 AM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 11:29 AM
RE: Help with Classes - by buran - Feb-16-2019, 11:46 AM
RE: Help with Classes - by Lonewolf - Feb-16-2019, 11:50 AM
RE: Help with Classes - by Lonewolf - Feb-17-2019, 03:17 PM
RE: Help with Classes - by Lonewolf - Feb-17-2019, 06:48 PM
RE: Help with Classes - by Lonewolf - Feb-18-2019, 08:07 AM
RE: Help with Classes - by Lonewolf - Feb-18-2019, 05:35 PM
RE: Help with Classes - by sonedap - Feb-18-2019, 08:44 PM
RE: Help with Classes - by sonedap - Feb-19-2019, 06:56 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020