Skip to content

Instantly share code, notes, and snippets.

@mitchute
Created January 25, 2019 16:18
Show Gist options
  • Save mitchute/5a2c32ee68a1054a5e872b77f37aea16 to your computer and use it in GitHub Desktop.
Save mitchute/5a2c32ee68a1054a5e872b77f37aea16 to your computer and use it in GitHub Desktop.
"""
Python will allow you to use any naming convention you want. And therefore, will not enforce
any naming style. However, there are some generally agreed upon "best practices"
which help unify consistency across Python programs.
Python formatting guidelines are outlined in the PEP8 style guide.
https://www.python.org/dev/peps/pep-0008/
Here is a basic formatting example adapted from here: https://www.journaldev.com/14633/python-inheritance-example
"""
# Classes should use a CamelCase naming style
class PersonClass(object):
# Initialize this class
def __init__(self, name, age, shoe_size, net_worth=None):
# Descriptive comment block.
"""
Person class. Provides base functionality for a person.
:param name: person's name
:param age: person's age, in years
:param shoe_size: person's shoe size, in US standard sizing
:param net_worth: person's net-worth, in US Dollars
"""
"""
Methods that use the double underscore, such as this initialization method,
are reserved for built-in Python methods. Therefore, we should not create new ones.
Often these are referred to as "dunder" methods, or "magic" methods.
"""
# Initialize any class variables
self.name = name
self.age = age
self.shoe_size = shoe_size
"""
Class variables start with "self" because they are part of a class
These should be named using snake_case.
Feel free to be as descriptive as needed when naming variables.
For example: borehole_thermal_resistance, or seasonal_performance_factor_4, etc.
"""
# Private variables
self._net_worth = net_worth
"""
Variables that start with an underscore, such at this one, are generally understood to be private
to the method. However, there's nothing from stopping anyone from accessing this data.
Generally, whenever you see one of these, you should stop and double check that it is actually
the value you are looking for, and not some intermediate variable.
"""
# Define any class methods, a.k.a. "functions"
def say_hello(self):
# Comment block describing the function, its input parameters, and what it returns.
"""
This method says 'Hi!' and introduces the person.
:return: introduction, as string
"""
"""
Functions also adopt a snake_case naming style
Try to keep functions as simple as possible so they can be easily tested.
Python also makes string formatting simple. See here for a guide on how to format strings.
https://pyformat.info/
"""
# Return whatever it is this function does
return 'Hi! My name is {} and I am {} years old.'.format(self.name, self.age)
# Define another class.
# This one inherits the PersonClass to reuse its attributes and methods.
class StudentClass(PersonClass):
# initialize the class
def __init__(self, name, age, shoe_size, rank, net_worth=None):
"""
This class describes a student
:param name: person's name
:param age: person's age, in years
:param shoe_size: person's shoe size, in US standard sizing
:param rank: person's class rank
:param net_worth: person's net-worth, in US Dollars
"""
# initialize any parent classes
PersonClass.__init__(self, name, age, shoe_size, net_worth)
# initialize other variables
self.class_rank = rank
# Define constants
self.NUM_SECONDS_IN_HOUR = 3600
"""
Constants should use an UPPER_CASE_WITH_UNDERSCORES naming style.
Again, Python won't enforce this, but it's typical to see this style.
"""
# Define more class methods
def report_class_rank(self):
"""
Report class rank
:return: A description of my class ranking
"""
return 'I am ranked no. {:d} in my class of peers.'.format(self.class_rank)
def introduce_myself(self):
"""
Introduce the student
:return: student introduction, printed to the terminal
"""
print(self.say_hello())
print(self.report_class_rank())
def calc_seconds_in_hours(self, hours):
"""
Computes the number of seconds in any given number of hours
:param hours: number of hours for which to compute the number of seconds
:return: seconds
"""
return self.NUM_SECONDS_IN_HOUR * hours
# Protect this code from being executed upon import
if __name__ == "__main__":
"""
See here for a more thorough description of what this does.
https://stackoverflow.com/a/419185/5965685
"""
# Run our code
Max = StudentClass(name='Max', age=20, shoe_size=10, rank=1, net_worth=100)
Max.introduce_myself()
hours = 10.2
print('There are {:0.0f} seconds in {:0.1f} hours.'.format(Max.calc_seconds_in_hours(10.2), hours))
@mitchute
Copy link
Author

Upon running, this returns:

Hi! My name is Max and I am 20 years old.
I am ranked no. 1 in my class of peers.
There are 36720 seconds in 10.2 hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment