-1

I need some help defining certain terms. I've been programming for a few months (python and JavaScript) and still have certain troubles understanding some of the programming terms

This is what am I working on now:

from datetime import datetime
now = datetime.now()
month1 = now.month
year1 = now.year
day1 = now.day
print "%s/%s/%s" % (month1, year1, day1)

It's very straight forward and it's easy to perform and understand, but I have no idea what classes, functions and methods are. Every time I Google an answer it's usually some big definition using even more programming words I don't understand. I need some basic definitions and preferable some examples. I hate having to ask this kind of question and any help would be much appreciated. Thanks.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Randy Newman
  • 1
  • 1
  • 1
  • 2
  • 2
    I honestly thought methods and functions were the same thing until I read this: http://stackoverflow.com/a/155655/16959 (this should answer all three of your questions) – Jason Sperske Jun 04 '13 at 22:26
  • 4
    method is just a function attached to a class. `datetime` is a class. `now` variable is a instance of the `datetime` class – cmd Jun 04 '13 at 22:30

3 Answers3

1
def thisIsAFunction(inputNumber):
     oneMore= inputNumber+ 1
     return oneMore

this is a function, that returns one more than the input number.

so later on in your code you call it like this

anotherVariable = thisIsAFunction(5)

then when you want to: print anotherVariable and it will print 6

classes are similar, but more broad and encompasing

class thisIsaClass:
     someVar = 1
     def __init__(self, inputNumber):
           self.inputNumber = inputNumber

this just defined an object called "thisIsaClass".... to create an object....

>>> a = thisIsaClass(1) #creating one 'thisisaclass' object called a
>>> b = thisIsaClass(2) #another called b
>>>
>>> a.inputNumber # getting inputnumber from a
1
>>> b.inputNumber
2
>>> a.someVar #someVar doesnt have 'self' in front of it so its not referring to its own someVar, its referring to its classes someVar
1
>>> b.someVar
1
>>> thisIsaClass.someVar = 3 
>>> a.someVar #see? it changed it for all of th someVars
3
>>> b.someVar #because its the same someVar
3
>>> thisIsaClass.inputNumber #and THIS one doesnt actually have a "inputNumber" because it only belongs to the actual objects
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class thisIsaClass has no attribute 'inputNumber'

if thats confusing, then the best way to look at a class is to relate it to people.

class human:
     def __init__(self, name, height, age):
          self.name = name
          self.height = height
          self.age = age


beth = human('beth','64 inches', 19)
beth.age
19

the __init__ is a METHOD, because it is a definition within a class. (Honestly i have no idea why its called that... but it is) if we were to define another function(but its in a class so its a method lol) below init like

    def birthday(self):
        self.age = self.age+1

then everytime we wanted to give beth a birthday, we would just type

beth.birthday()
beth.age
20
TehTris
  • 3,139
  • 1
  • 21
  • 33
1

A function is a thing that can do something and/or return a result, which depends on the parameters you invoke it with.

How you make and use a function:

def function(foo):
    ...

result = function("bar")

You asked about a class, but first I'll explain an object.

An object contains some data and can do some things. The things it can do are functions, and functions associated with an object are in particular called methods. In addition to the parameters, the behavior of a method can, and usually does, depend on the object's data, and can update the data. In some programming languages, you cannot directly access the data in an object or maybe some but not all of it; you can only ask the object to do the things it does, and it might or might not offer setting the value of its data as one of the things it does. However, in Python you can directly access the data, but some still consider it good practice not to.

A class is a group of objects that have the same type of data and same set of things that they can do. It also provides a starting point to create new objects of that class.

How to make a class with a method:

class Foo:
    def method(self, parameter):
        ...

How to make an object of the class:

f = Foo()

How to call a method of the object:

f.method(argument)
morningstar
  • 8,952
  • 6
  • 31
  • 42
0

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671