1

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

2 Answers 2

5

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!')
Sign up to request clarification or add additional context in comments.

1 Comment

every molecule will belong to some system. If i use the method that you have stated, how do I pass parameters for the molecule ? please try to answer in terms of my class structure or propose a change in the class structure if you have a better solution for the same
0

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.

7 Comments

Consider me having 2 systems, System1 and System2 and 2 molecules mol1 and mol2. then how do i define which system does a molecule belong to >
inheritance models relationship 'extend a' between classes. You are trying to model a 'has a' so in my view you molecules instances should be attributes of system instances, rather than the molecule classes being a child class of system
Each molecule has its own attributes like position, but molecules in the same system can have the same temperature even though not the position. Definitely a extension
the answer from @progmatico is correct, and it does not make use of inheritance
That solution isn't entirely correct because I can't just call molecule. Temp , but the system of defining modules by function solves my problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.