YUP Tutorial Module 0: Preparations

3. Objects

Most molecular modeling programs, can be viewed as a collection of procedures (functions or subroutines if you like). YUP is different; it is a collection of objects. Hence, part of YUP is implemented in Python, an object-oriented programming language. To implement a new molecular model type in YUP, you will have to write Python programs to create and manipulate YUP objects.

Sections

1. Introduction
2. Python
3. Objects
4. Exercises
You have already studied classes in Chapter 9 of the Python Tutorial. If you are still not clear what classes or objects are, or if you want to learn more, there are some online tutorials available:

The shortest and best tutorial is Chapter 11: Object-Oriented Programming from A Byte of Python. (There are two omissions in the code example on the Class and Object Variables page; you should be able to fill in the missing code.)

A more detailed and longer tutorial is Chapter 5: Objects and Object-Orientation from Dive into Python. Unfortunately, the opening page will put many people off. Do not worry too much about comprehending the code. It will be explained in subsequent pages that are quite easy to understand.

All types of objects are treated in very similar ways. This means you should be able to learn to use new object types very quickly. Let us look at the life-cycle of objects in Python:

1. Creation

The syntax to create an object of a built-in type is quite diverse. Some examples:

a = 5
b = 'box'
c = {}
d = float( 7 ) #--- or: d = 7.0

An integer of value 5 was created and assigned to the variable a. A string 'box' was assigned to the variable b. An empty dictionary was created and assigned to the variable c. Finally, a floating point number with the value 7.0 was created and assigned to the variable d.

The syntax used to create the last object is also used for the creation of user defined types. For example:

from UserList import UserList
e = UserList()

The import statement in the first line is necessary because UserDict and UserDict.UserDict are not built-in objects. All YUP object types are created by calling a constructor function as in the above example.

2. Attributes

An object may have data attributes. These are accessed by attaching a period followed by the name of the attribute to the object or the variable that contains the object.

''.__class__
b.__class__
e.data

Every object has an attribute named __class__. An object of the UserList type has an attribute named data and the above example shows e to be an empty list.

Many attributes are read-only and you can assign new values to the others. For example:

e.data = []

which assigns a new value which happens to be an empty list to the object e whose original value was also an empty list. (Note that this is not how you would add objects to a UserList object.)

The properties of most YUP objects can be queried through the appropriate attributes. Some attributes can be assigned new values but many attributes are read-only.

3. Methods

Like attributes, methods are accessed using the dot notation:

b.upper()

What makes this a method call is the set of parentheses. If you leave them out, you will get a printout of the attribute.

The previous example returns another sequence object (a string in this case) and we can apply an appropriate method to that too:

b.upper().lower()

and it is reassuring that the methods are applied in the order we expect.

YUP objects are manipulated mostly by calling an appropriate method, i.e. users indicate their desired calculations by making method calls. Properties that have read-only attributes can be set only by calling an appropriate method.

4. Mapping

Mappings use brackets. An indexing (or subscripting) example:

b[2]

It is possible to assign new values to a mapping. For example:

c[b] = a
c['box']

The string b is a key to the value, the number a. The second line confirms the mapping.

Some YUP objects define mappings that can be quite unusual. Some of these mappings use other YUP objects as keys.

5. Operators

Operators are defined for some object types. As one would expect, a large number of operators have been defined for number objects, for example:

a + 3
a - 1
a * 4
a / 2

Fewer operators are defined for sequence objects. For example:

b + b
b * 3

Try the subtraction and division operator on the string object.

Most YUP objects do not define operators. The notable exception is the AtomVector object which is used to represent vector quantities such as coordinates and velocities.

6. Statements

Objects can be used elsewhere. For example, as parameters in function calls:

len( b )
type( {} )

We use the variable holding a string object in the first statement. A dictionary object is used directly in the second statement.

7. Deletion

An object can be deleted if it is no longer needed. The attributes of an object may also be deleted, but attributes of built-in object types are usually protected from deletion.

del a

Some object types offer a method that users can use to indicate that they are done. For example, apply the close method to the file object. The file object is not deleted but it is no longer very useful.

Some YUP object types provide a finish method that has much the same effect as the close method.
Modules

0. Preparations
1. Model Construction and Simulations

2. Programming with YUP Part 1
3. Programming with YUP Part 2
4. Force field Assembly
5. Developing Potential Energy Terms

Now that you have seen how objects are used in Python, you should have no problems working with YUP objects. When you are faced with a new type of object, find out what the attributes and methods are, what mapping if any has been defined for it, and what operators are defined for the type. Check the enclosing module (from where you import the constructor function) for functions that operate on the new type.
Resources

Yammp Under Python
The Harvey Laboratory

Python Home

Python Tutorial

(new browsers)
Prev | Top | Next