Creating Objects
Python is an object-oriented language. All data items in Python are objects. In Python, data items that could be thought of as similar are named by a type or class. The term type and class in Python are synonymous: they are two names for the same thing. So when you read about types in Python you can think of classes or vice versa.
There are several built-in types of data in Python including int, float, str, list, and dict which is short for dictionary. You can also get help for any type by typing help(typename) in the Python shell, where typename is a type or class in Python.
for example : >>> help(int)
for example : >>> help(int)
A very good language reference can be found at http://python.org/doc, the official Python documentation website.
reference = another reference (or) object
Non-literal Object Creation
variable = type(other_object_values)
Literal Values
There are two ways to create objects in Python. In a few cases, you can use a literal value to create an object.
Literal values are used when we want to set some variable to a specific value
within our program. For example, the literal 6 denotes any object with the
integer value of 6.
x = 6
This creates
an int object containing the value 6.
It also points the reference called x at this object as pictured in Fig -1. All assignments in Python point references at objects.
Any time you
see an assignment statement, you should remember that the thing on the left
side of the equals sign is a reference and the thing on the right side is
either another reference or a newly created object. In this case, writing x = 6 makes a new
object and then points x at this object.
Other
literal values may be written in Python as well. Here are some literal values that
are possible in Python.
• int literals: 6, 3, 10, -2, etc.
• float literals: 6.0, -3.2, 4.5E10
• str literals: ‘hi there’, “how are you”
• list literals: [], [6, ‘hi there’]
• dict literals: {}, {‘hi there’:6, ‘how are
you’:4}
Python lets
you specify float literals with an exponent. So, 4.5E10
represents the float 45000000000.0. Any number written with
a decimal point is a float, whether
there is a 0 or some other value after the decimal point. If you write a number
using the E or exponent notation, it is a float as well.
Any number without
a decimal point is an int, unless it
is written in E notation.
String literals are surrounded by either single or
double quotes.
List literals are surrounded by [ and ]. The [] literal represents the
empty list.
The {} literal is the empty dictionary. A dictionary is a mapping of keys to values. In the dictionary literal, the key ‘hi
there’ is mapped to the value 6, and the key ‘how are you’ is mapped to 4.
Non-literal Object Creation
when an object is
created, sometimes it may not created from a literal value. It can be created
from one or more existing objects. It is called Non-literal Object Creation.
For instance, if we have
a string in Python, like ‘6’ and want to create
an int object from that string, we can do
the following.
y = ’6’
# ‘6’ is a string and assigned to y
x = int(y)
# value of y is converted to integer
print(x) # now x = 6
, i.e, it becomes int literal
In this short piece of
code, y is a reference to the str object
created from the string literal. The variable x is a reference to an
object that is created by using the object that y refers to. In general,
when we want to create an object based on other object values we write the
following:
The type is any type or class name in Python, like int, float, str or any other type. The other_object_values is
a comma separated sequence of references to other objects that are needed by
the class or type to create an instance (i.e. an object) of that type. Here are
some examples of creating objects from non-literal values.
z = float(’6.3’) # ‘6.3’ is a string, it is converted to
float and assigned to z
w = str(z) # z value converted to string and
assigned to w
u = list(w) # this results in the list [’6’, ’.’, ’3’]
Calling Methods on Objects
Objects
are useful because they allow us to collect related information and group them
with behaviour that act on this data. These behaviours are called methods in Python.
There
are two kinds of methods in any object-oriented language: mutator and accessor methods.
Accessor methods
access the current state of an object but don’t change the object. Accessor
methods return new object references when
called.
x
= ’how are you’
y
= x.upper()
print(y)
Here,
the method upper is
called on the object that x refers
to. The upper accessor method returns a new
object, i.e, a str object,
that is an upper-cased version of the original string.
Note
that x is not
changed by calling the upper method
on it. The upper method
is an accessor method.
There
are many accessor methods available on the str type.
Some
methods are mutator methods. These methods
actually change the existing object. One good example of this is the reverse method
on the list type.
myList
= [1, 2, 3]
myList.reverse()
print(myList)
# This prints [3, 2, 1] to the
screen
The
reverse method
mutates the existing object, in this case the list that myList
refers to. Once called, a mutator method can’t
be undone. The change or mutation is permanent until mutated again by some
other mutator method.
All
classes contain accessor methods. Without
accessor methods, the class would be pretty uninteresting. We use accessor
methods to retrieve a value that is stored in an object or to retrieve a value
that depends on the value stored in an object.
If a class had no accessor methods we could put values in the
object but we could never retrieve them.
Some
classes have mutator methods and some don’t. For
instance, the list class
has mutator methods, including the reverse method.
There
are some classes that don’t have any mutator
methods. For instance, the str class
does not have any mutator methods.
When a class does not contain any mutator methods, we say that the
class is immutable. We can form new values
from the data in an immutable class,
but once an immutable object is created, it cannot be changed. Other immutable
classes include int and
float.
Implementing a Class
Programming in an object-oriented language usually means
implementing classes that describe objects which hold information that is
needed by the program you are writing.
Objects contain data and methods operate on that data. A class is the definition of the data and methods for a specific type of object.
Every class contains one special method called a constructor. The constructor’s job is to create an instance of an object by
placing references to data within the object itself.
For example, consider a class called Dog. A dog has a name, a
birthday, and a sound it makes when it barks. When we create a Dog object, we
write code like that appearing as below.
No comments:
Post a Comment