Reputation: 1521
I wish to make a class System with arguments : sysname
,temp
,N
and make a child class Molecule which uses inheritance to get parameters from System.
class System() :
def __init__(self,sysname,temp,N):
self.sysname=sysname
self.temp=temp
self.syslist=[]
self.N=N
class Molecule(System) :
def __init__(self,name,position,mass,radius):
self.name=name
self.position=position
self.mass=mass
self.radius=radius
self.epsilon=1
self.sigma=1
self.syslist.append(self)
I can make an instance of System as
System1=System("system1",100,10)
System2=System("system2",120,10)
Now while defining a molecule, how do I a define which system does it belong to?
Also, please tell if there is something else wrong with my class definitions
Upvotes: 1
Views: 90
Reputation: 4964
Maybe your molecules belong to a system and do not inherit from it (you are stating a wrong solution but you did not explain precisely your problem in terms of class relations).
If this is the case you can just add the system as an attribute of molecules.
class Molecule:
def __init__(self, system):
self.system = system
...
and then you write
sys1 = System()
mol1 = Molecule(sys1)
Edit:
You just add the parameters you want. Also you choose which class will keep references to the other based on your intentions. The lists you are keeping inside the class may be better kept outside (or not) see the code below. There I have a list of systems. Each system has a list of molecules. Molecules don't know about their system (do they need to know? Their system knows about them; my first example was reversed, Molecules knew about their system, that's why you needed to create them by passing also their system reference, together with your parameters). In this case that is not needed.
class System:
def __init__(self, name,temp,N):
self.molecules = [] # list with the system molecules
self.name = name
self.temp=temp
self.N=N
def add_molecule(self, molecule):
self.molecules.append(molecule)
def add_molecules(self, molecule_list):
self.molecules.extend(molecule_list)
class Molecule:
def __init__(self, name, position, mass, radius):
self.name=name
self.position=position
self.mass=mass
self.radius=radius
self.epsilon=1
self.sigma=1
system_list = []
system1 = System("system1",100,10)
system2 = System("system2",120,10)
system_list.extend([system1, system2])
# example
for system in system_list:
print(system.name)
print()
# now some molecules for system1, you need to keep references to them,
# isolated or in list. Each system will know its molecules.
mol1 = Molecule('H2O', (0,0,0), 18, 200) # your values
mol2 = Molecule('CO2', (10,10,10), 44, 300)
mol3 = Molecule('O3', (10,10,10), 18*3, 600)
system1.add_molecule(mol1) # add molecules to a system
system2.add_molecules([mol2, mol3])
# example
for system in system_list:
print(system.name)
for molecule in system.molecules:
print(molecule.name)
print()
if mol1 in system1.molecules:
print('Found water here!')
Upvotes: 5
Reputation: 5327
each molecule instance will have all the attributes of System, nothing to do with other System instances. you are confusing instance and definition of an object.
Upvotes: 0