Summary

Now it's not too difficult to understand:

Class is a blueprint to create instances/objects of that class. (I like this way to say it: if class is the cooikie cutter, then instance objects are the cookies that are the result of instantiating class.)

Objects, created from a class, have attributes defined in the class. Accessing these attributes using: objectName.attribute

Attributes include two kinds:

data attributes are variables that belong to an object or class; function attributes, functions that defined in a class. Such functions are called methods.

Simply put, methods are functions that “belong to” an object. They always have the first parameter self referring to the object itself.

When you call a method you do not give a value for self parameter, you only provide values for other parameters if needed.

An example

# define a class
class Duck:
  name = 'Donald'
  color = "brown"

  def say(self):
    print("I am a duck.")
  def makeSound(self, sound):
    print("I make sound: " + sound)

# create an object of the class
d = Duck()

# access the data attributes of the object
print(d.name)      # Donald
print(d.color)     # brown

# access the function attributes/the object methods 
d.say()                   # I am a duck. 
d.makeSound("Quark!")     # I make sound: Quark!

When we call the object methods, we don't need to provide value for the first parameter self, Python will automatically provide the object reference. So d.say() has no input value in the parenthesis. In d.makeSound("Quark"), we provide string value "Quark" for variable sound.

results matching ""

    No results matching ""