The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Question about _this()
Discuss Question about _this() in the Python Programming forum on Dev Shed. Question about _this() Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 7th, 2013, 06:23 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 25 m 18 sec
Reputation Power: 0
|
|
|
Question about _this()
In the following code ( A molecule includes several atoms.):
for j in range(mol.getNbAtoms()):
mol.atoms[ j ]._this()
mol = mol._this()
my question is: what is the meaning of the Line 2. If _this() returns an atom, where will this atom be return?
These codes are from a program written by other people, So I don't understand it quite well.
Thanks for answers!
|

January 7th, 2013, 09:49 AM
|
 |
Contributing User
|
|
|
|
|
Guessing, you've shown hardly any code.
mol is an object representing some molecule. Let's call it carbon dioxide.
mol.getNbAtoms()
getNbAtoms is a method of the Molecule class. It returns the number of atoms in the molecule. 3 for CO_2.
mol.atoms is a an object with a __getitem__ method. In other words, it supports the [] indexing notation.
mol.atoms[ 0] would return an Atom object, say, for oxygen.
mol.atoms[1] likewise is an Atom object, say, carbon.
mol.atoms[2] would represent another oxygen. Could be the same object as mol.atoms[0], or a different one if the simulation accounts for the state of each atom.
_this is a method. A function that need not takes any parameters.
Molecule._this might be different from Atom._this .
Programming experiment. Before running the program again insert statements like
print('mol._this')
print(help(mol._this))
print(''atom 0 ._this')
print(help(mol.atoms[0]._this))
for j in range(mol.getNbAtoms()):
...
Summary: I think your interpretation of the program is incorrect. mol.atoms[j] represents the atom. Furthermore, without more code I'm clueless, this entire explanation may be an exercise in bogosity.
__________________
[code] Code tags[/code] are essential for python code!
|

January 7th, 2013, 11:01 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 25 m 18 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by b49P23TIvg Guessing, you've shown hardly any code.
mol is an object representing some molecule. Let's call it carbon dioxide.
mol.getNbAtoms()
getNbAtoms is a method of the Molecule class. It returns the number of atoms in the molecule. 3 for CO_2.
mol.atoms is a an object with a __getitem__ method. In other words, it supports the [] indexing notation.
mol.atoms[ 0] would return an Atom object, say, for oxygen.
mol.atoms[1] likewise is an Atom object, say, carbon.
mol.atoms[2] would represent another oxygen. Could be the same object as mol.atoms[0], or a different one if the simulation accounts for the state of each atom.
_this is a method. A function that need not takes any parameters.
Molecule._this might be different from Atom._this .
Programming experiment. Before running the program again insert statements like
print('mol._this')
print(help(mol._this))
print(''atom 0 ._this')
print(help(mol.atoms[0]._this))
for j in range(mol.getNbAtoms()):
...
Summary: I think your interpretation of the program is incorrect. mol.atoms[j] represents the atom. Furthermore, without more code I'm clueless, this entire explanation may be an exercise in bogosity. |
Thanks for your patiently reply!
Because it takes several pages to post the codes, I choose not to do that. Your guess actually makes sense.
The Line 3 is quite clear for me. For Line 2, I expect a line like this:
mol.atoms.append( NewAtom)
There is no implementation of _this() in Atom class, which makes me dizzy.
Your advice is quite useful, I will try and see what is inside _this().
Thanks!
|

January 7th, 2013, 11:46 AM
|
 |
Contributing User
|
|
|
|
|
_this might be defined in a parent class which you could find via source that looks like
class SubClass(ParentClass):
Likely that mols.atoms is a list.
print(type(mols.atoms)) # indicator
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|