Home Python: Defining class with dependent attributes
Reply: 0

Python: Defining class with dependent attributes

user1673
1#
user1673 Published in May 25, 2018, 8:57 am

I aim to write a class who can be used to calculate all the properties of a device.

import numpy as np

class pythagoras:
def __init__(self, a=None, b=None, c=None):
self.a = a
self.b = b
self.c = c

if(a == None):
assert(b != None)
assert(c != None)
self.a = np.sqrt(c**2 - b**2)
elif(b == None):
assert(a != None)
assert(c != None)
self.b = np.sqrt(c**2 - a**2)
elif(c == None):
assert(a != None)
assert(b != None)
self.c = np.sqrt(a**2 + b**2)
else:
assert (a**2 + b**2 == c**2), "The values are incompatible."

example1 = pythagoras(a=3, b=4)
print(example.c)
# 5
example2 = pythagoras(a=3, c=5)
print(example2.b)
# 4
example3 = pythagoras(b=4, c=5)
print(example3.a)
# 3


So my question is about simplifying this example: Is there a simpler way to implement this kind of problem? For more complex examples it gets quickly rather complex and unmanageable.

Application

The final goal is to have a class with all device properties such as:

class crystal:
absorption
refractive_index
transmission
reflection
heat_conductivity
heat_resistance


Here one can imagine that these properties depend on each other and according to my knowledge of properties, I can infer the rest of the properties.

I am graceful for any remarks about writing better code. Even though I learned and read literature about object-oriented coding, I am inexperienced in writing such.

 You need to login account before you can post.
Processed in 0.305063 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO