vidallia avatar

[Python3] I made my first class! Feedback please.

vidallia

Published: 07 Jul 2017 › Updated: 07 Jul 2017[Python3] I made my first class! Feedback please.

[Python3] I made my first class! Feedback please.



Well, I just started a course on OOP with Python. Up until this point I had a lot of trouble grasping the concepts of, attributes and objects relating to classes but, I think I got it.  


Can any Python experts comment below and let me know what you think about my first useful class with methods.

# This is a test class made on 7/6/2017.
# Creates a class called User and sets, attributes for name and birth.
# Calculated how old user is and if their "oldenough" < 25 returns False.
# Alot of this is to just test things out
class User:
 #Birthday in yyyymmdd format
 def __init__(self, name, birth):
     self.name = name
     self.birth = birth
     self.byear = birth[0:4]
 #Checks year current year minus year born returns year born
 def yearsold(self):
     year = 2017
     age = year - int(self.byear)
     return age
 #returns False if younger than 25, and True for older than 25   
 def oldenough(self):
     if self.yearsold() < 25:
         return False
     else:
         return True
#Assigning an object called Ted to User with the required attributes
ted = User("Ted", "19941013")
rick = User("Rick", "18011230")
print(ted.name)
print(ted.byear)
print(ted.yearsold())
print(ted.oldenough())
print('.' * 25)
print(rick.name)
print(rick.byear)
print(rick.yearsold())
print(rick.oldenough())
#checks if the user is old enough 
#just wanted to test things out
if ted.oldenough() != True:
 print("Sorry {} you're only {} years old!".format(ted.name,ted.yearsold()))
else:
 print("Access Granted to: {}".format(ted.name))

Leave [Python3] I made my first class! Feedback please. to:

Written by

Technology, Blockchain, Computer Science. Those are some of my favorite things.

Read more #python posts


Best Posts From vidallia

We have not curated any of vidallia's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From vidallia