Python: Defining class with dependent attributes

strpeter Source

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.

pythonpython-3.xclass

Answers

answered 3 months ago Games Brainiac #1

I think this is rather easy to calculate once you use the @property decorator. Let me show you an example.

import math

class Pythagoras(object):

    def __init__(self, a=None, b=None, c=None):
        self._a = a
        self._b = b
        self._c = c

        count = 0
        if self._a is None:
            count += 1
        if self._b is None:
            count += 1
        if self._c is None:
            count += 1

        if count > 1:
            raise Exception("More than one of the values are None.")

    @property
    def a(self):
        if self._a is None:
            return math.sqrt(self.c**2 - self.b**2)
        else:
            return self._a

    @property
    def b(self):
        if self._b is None:
            return math.sqrt(self.c**2 - self.a**2)
        else:
            return self._b

    @property
    def c(self):
        if self._c is None:
            return math.sqrt(self.a**2 + self.b**2)
        else:
            return self._c

Working behavior:

>>> from temp import Pythagoras
>>> p = Pythagoras(a=10, b=20)
>>> p.c
22.360679774997898

Edit: Updated the code to make sure that it was working.

answered 3 months ago Cezary.Sz #2

Try this:

import pandas as pd

class Pyth():
    def __init__(self, a=np.NaN, b=np.NaN, c=np.NaN):
        df = pd.DataFrame({
           'sides': [a,b,c], 
           'equation':[np.sqrt(c**2 - b**2), np.sqrt(c**2 - a**2), np.sqrt(a**2 + b**2)]
        })
        df.loc[df.sides.isnull(), 'sides'] = df[df.sides.isnull()]['equation']

        self.a = df.sides[0]
        self.b = df.sides[1]
        self.c = df.sides[2]

Test result:

test = Pyth(a=3,b=4)
test.c
Out[217]:
5.0
In [218]:

test.b
Out[218]:
4.0
In [219]:

test.a
Out[219]:
3.0

comments powered by Disqus