Computer Science, asked by kumarop998, 1 year ago

What are best practices to define Python classes?

Answers

Answered by AdityaNirupam
0

Composed method

Divide program into methods that perform one task

Keep all operation in a method at the same level of abstraction

Use many methods only a few lines long

Different levels of abstraction: bitmaps, filesystem operations, playing sounds...

# Better

class Boiler:

def safety_check(self):

if any([self.temperature > MAX_TEMPERATURE,

self.pressure > MAX_PRESSURE]):

if not self.shutdown():

self.alarm()

def alarm(self):

with open(BUZZER_MP3_FILE) as f:

play_sound(f.read())

@property

def pressure(self):

pressure_psi = abb_f100.register / F100_FACTOR

return psi_to_pascal(pressure_psi)

...

view rawgistfile1.py hosted with ❤ by GitHub

safety_check deals with temp and pressure

alarm deals with files and sound

pressure deals with bits and converting them

Similar questions