It may be easier to understand everything if you define your own classes, instances, methods, and functions.
class Car(object):
def set_odometer(self, miles):
self.miles = miles
def get_odometer(self):
return self.miles
def drive(self, miles):
self.miles += miles
def joyride(stolen_car):
stolen_car.drive(100)
print 'Insert Yello song here'
new_car = Car()
new_car.set_odometer(0)
print 'Now it has', new_car.get_odometer(), 'miles!'
joyride(new_car)
print 'Now it has', new_car.get_odometer(), 'miles!'
So:
Car
is a class. A class defines a new type. Just as int
defines integers, and str
defines strings, Car
defines a new kind of thing that you can drive
(and do other things to).
Each time you create a new instance of the Car
class, it's an object. For example, new_car
is an instance of Car
. That is, it's a thing you can drive
(and do other things to).
Car.drive
is a method of the Car
class. This means that new_car.drive
is a method of the new_car
instance. Notice the self
parameter in the definition of drive
. When you call new_car.set_odometer(100)
, that self
parameter refers to new_car
.
joyride
is a function. It's not part of any class, and it has no self
parameter. That's really the only difference between methods and functions.
(In fact, under the covers, in Python, a method is just a way of putting a function and an instance together. For example, new_car.drive.im_self
is new_car
. But don't worry about that.)
Notice that inside of joyride
, stolen_car
is the same object that you passed in—in this case, new_car
. So, you can call stolen_car.drive()
, and it's driving new_car
.
In real code, you usually won't bother with methods like set_odometer
and get_odometer
, because you can access data attributes, aka members, like miles
just as easily as you can access methods:
new_car = Car()
new_car.miles = 0
print 'Now it has', new_car.miles, 'miles!'
However, you do often set initial values for your attributes in a special __init__
method. For example:
class Car(object):
def __init__(self):
self.miles = 0
def drive(self, miles):
self.miles += miles
def joyride(stolen_car):
stolen_car.drive(100)
print 'Insert Yello song here'
new_car = Car()
print 'Now it has', new_car.get_odometer(), 'miles!'
joyride(new_car)
print 'Now it has', new_car.get_odometer(), 'miles!'
So, how does that apply to your example?
Well, your example is a bit confusing. You don't call any normal instance methods at all. But let's go through it step by step.
from datetime import datetime
datetime
is a class. How do you know that? Well, you could look at the code inside the datetime
module, but, more simply, just print it:
>>> from datetime import datetime
>>> print datetime
<type 'datetime.datetime'>
Loosely speaking, a type
and a class
are the same thing. (A type
can actually be written in custom C code, instead of with a class
, and so on, so this isn't exactly true, but it's close enough.)
now = datetime.now()
datetime.now
is a special kind of thing known as a classmethod
. Normal methods can only be called on instances, but class methods are instead called on classes. Don't worry about this for now. The important thing is that what it returns is an object, an instance of type datetime
. Again, you can test that, by printing out its type
:
>>> now = datetime.now()
>>> print type(d)
<type 'datetime.datetime'>
And then you just access some data attributes of the object:
month1 = now.month
year1 = now.year
day1 = now.day
These attributes were set by the now
method, much the same way that a normal class sets attributes inside its __init__
method.