I have a class representing an Atom, and it has two sets of coordinates, one of these sets is cartesian, and I would like to follow best architectural practices to get that information out of the class.
Previously I have created multiple class methods for getting different types of attributes, but I feel this isn't pythonic, and is unnecessarily cluttering my code, however I am not sure what the solution would generally be.
As it may help someone to answer, please see the Atom class below:
class Atom():
def __init__(self, element, x_coord, y_coord, z_coord):
'''
Instantiate new atom object.
'''
#Check information is correct
if not isinstance(element, str):
raise ValueError('Element must be a string.')
if not isinstance(x_coord, int) and not isinstance(x_coord, float):
raise ValueError('All coordinates must be numeric, x is not.')
if not isinstance(y_coord, int) and not isinstance(y_coord, float):
raise ValueError('All coordinates must be numeric, y is not.')
if not isinstance(z_coord, int) and not isinstance(z_coord, float):
raise ValueError('All coordinates must be numeric, z is not.')
self.coordinates = np.array([x_coord, y_coord, z_coord]).astype(float)
self.cartesian = None
self.element = element
def __repr__(self):
'''
Atom representation.
'''
x, y, z = list(self.coordinates)
return f"Atom('{self.element}', {x}, {y}, {z})"
def __copy__(self):
x, y, z = [float(x) for x in list(self.coordinates)]
return Atom(self.element, x, y, z)
def set_cartesian(self, vector_space):
'''
Set the cartesian coordinates of the atom, based on a vector space -
generally that of a unitcell. Requires column vectors.
'''
self.cartesian = vector_space @ self.coordinates.T
I would preferably like a single method to handle all attributes, to keep the class uncluttered. I understand I could just use "class.attribute", but as I understand it this can cause code to break long term if you ever want to rename something.