As a simple example, let's assume that we want to create a lot of Earthquake
instances, having name, origin time, and hypocenter coordinate attributes coming from other sources encoded as strings ("Nepal 25-4-2015T11:56:26 28.14 84.71 15.0"
).
class Earthquake(object):
def __init__(self, strline):
....
So, what we must do is:
parse the string to receive name, date, time, latitude, longitude and depth.
instantiate
Earthquake
by passing those values to initialization call__init__
.
Imagine the first part is done by a simple function:
import datetime as dt
def myfunc(strline):
items = line.split()
name = items[0]
otime = dt.datetime.strptime(items[1], "%d-%m-%YT%H:%M:%S")
lat, lon, depth = map(float, items[2:])
Now I want to use the class class Earthquake
to create Earthquake
objects in a way that each object has attributes Earthquake.name
, Earthquake.otime
, Earthquake.lat
, Earthquake.lon
and Earthquake.depth
.
How can I call myfunc
method in __init__
method in a class to initialize an object with above attributes?