Python Forum
Creating a script with a class have specific properties
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a script with a class have specific properties
#2
It would be useful to show us small sample of the text file. And also the code where you actually read and use the class you have defined. Note that what you show right now is the class definition. You need to create an instance of the class (i.e. instantiate particular object of that class).
As to the class definition it's more or less correct. The problem that I see is the if condition in the assessment method. Unless self.DHB is equal to 4.5 you will get an error because rate will not be defined when you try to use it on line 11. Also I don't think the logic is correct all together. if I understand your requirements you want to calculate DIB property always, not only when DHB == 4.5, right?
I am going to show you something more advanced. Because DIB is calculated property, it's better it to be sort of read-only (i.e. user will not be able to alter it.

class Tree:
    def __init__(self, block, plot, species, dhb):
        self.block = block
        self.plot = plot
        self.species = species
        self.dhb = dhb
 
    @property
    def dib(self):
        return self.dhb/1.09
        
    def report(self, report):
        return 'Report {}\nBlock: {}\nPlot: {}\nSpecies: {}\nDBH: {:0.3f}\nDIB: {:0.3f}'.format(report, self.block, self.plot, self.species, self.dhb, self.dib)
  
#example how to instanciate object of class Tree
#ignore numbers if they don't make business sense
  
tree = Tree(block=1, plot=5, species=7, dhb=200)
print(tree.report(1))
Output:
Report 1 Block: 1 Plot: 5 Species: 7 DBH: 200.000 DIB: 183.486 >>>
I changed variable names to be compliant with PEP8 style guide. also Tree.report() method is just an example. you can take it from here and play a bit with it
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
RE: Creating a script with a class have specific properties - by buran - Oct-11-2018, 06:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I need help writing this code for string properties and methods hannah71605 4 3,200 Mar-22-2021, 12:36 PM
Last Post: menator01
  Creating Email Message Class in Python QuavoJ 1 2,209 Jul-20-2020, 08:30 PM
Last Post: Yoriz
  Sort objects by protected properties. Sigriddenfeta 1 1,935 Mar-17-2020, 04:11 AM
Last Post: Larz60+
  Creating class with getters and setters. amandacstr 3 2,390 Jun-19-2019, 07:10 PM
Last Post: nilamo
  newbie questions about objects properties... slowbullet 2 2,711 Aug-12-2018, 01:12 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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