Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > BOOK: Python Projects
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Python Projects
This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Python Projects section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 27th, 2019, 10:51 AM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2019
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Class Property Page 30: Possible Shortcomings?

The circle Class seems to be designed to prevent its attribute radius from receiving negative values through the use of the property feature and a defensive setter __setRadius.

Whilst this prevents a caller from supplying a non positive value once the object has been created, it does not stop this happening when the object is created:
Code:
c1 = Circle(-30)
will set the radius to the negative value because the setter is not invoked in the initialiser.

Curiously, if you try to do this by changing
Code:
self.__radius = radius
to
Code:
self.__radius = self.__setRadius(radius)
, the interpreter does not complain but the assignment does not work with a positive value, instead radius is set to None. It does, however, work if you repeat the check / raising of ValueError for a non positive value.

I am also a little surprised that the design prevents the caller from reading the value of the radius when the property mechanism allows them to alter it, as this seems somewhat bizarre behaviour. This can be overcome by introducing a getter and altering the property assignment along the following lines:

Code:
def __getRadius(self):
        return self.__radius
    
    radius = property(__getRadius, __setRadius)
Reply With Quote
  #2 (permalink)  
Old January 27th, 2019, 01:37 PM
Wrox Author
Points: 119, Level: 2
Points: 119, Level: 2 Points: 119, Level: 2 Points: 119, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You are quite correct on all counts. The main issue of setting a negative radius on creation should be dealt with by raising a value error in the __init__() method if a negative value is received. The best way to do that would have been to call the property method directly from __init__(). That was careless of me.

The other points are valid but I did not think they would not have added much to the point of the exercise which was to demonstrate the value of properties. But it would have added extra code to the chapter which was only supposed to be a short refresher not a full tutorial. As an author you are constantly trying to minimise the amount of code needed to illustrate the current point. I chose to demonstrate that properties allow you to create write-only properties as well as read-only (and hopefully obviously, read-write!) But it was an entirely arbitrary choice.

Finally the reason why:

Code:
self.__radius = self.__setRadius(radius)
returns None is because None is the default return value of a Python function/method.
Instead of assigning it, you only need to call the method inside init:

Code:
def __init__(self,radius): self.__setRadius(radius)
Thanks for taklng the time to post.
Reply With Quote
  #3 (permalink)  
Old January 27th, 2019, 01:45 PM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2019
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Response on Property Class Query

Hi Alan.

Thanks for the prompt response. I can see that __setRadius does not return a value, and I should have picked this up myself.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Errata on page 30 regarding DinnerRepository Rushino BOOK: Professional ASP.NET MVC 2 9 January 24th, 2011 05:46 PM
Property access from Class within Partial Class zoltac007 C# 2005 0 December 1st, 2006 01:01 AM
Access to a property in the class. AyatKh ASP.NET 1.0 and 1.1 Basics 2 December 18th, 2003 11:25 AM
Project.Class.Property.? Jstmehr4u3 Pro VB 6 2 June 11th, 2003 11:29 AM



All times are GMT -4. The time now is 07:56 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.