Quantcast
Channel: Give a number units when used through a class - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Give a number units when used through a class

$
0
0

I'm trying to get a class to spit out a number with units. The purpose of it having units would be to be able to work with the number later, but without the units getting in the way as a string. Though I have no idea how to do this. I'm quite new with (actually understanding) classes, I'm not even sure if what I ask is possible or not.

My goal is to make a class for making atoms, based on their protons, electrons and neutons. The thing I wanna try is to be able to see the units of the mass (in kgs for my purpose) when I try printing it. Here's what I have:

    class Atom:        def __init__(self, protons, electrons, neutrons):            ElemCharge = 1.6021765*(10**-19)            UMAS = 1.66054*(10**-27)            self.protons = protons            self.electrons = electrons            self.neutrons = neutrons            self.charge = ElemCharge*(self.protons - self.electrons)            self.mass = (self.protons + self.neutrons)*UMAS    def main():        Hydrogen = Atom(1,1,0)        print (Hydrogen.mass)        Argon = Atom(18,18,22)        print (Argon.mass)    if __name__ == "__main__":        main()

This code works completely fine, so no worries about that :p

Is there a way to even do this? If so, how can it be done? Thanks!


Viewing all articles
Browse latest Browse all 5

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>