Search

Python Classes and Objects

Updated: Apr 27

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.


The difference is simple and conceptual. A class is a template for objects. ... An object is a member or an "instance" of a class. An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.


Python Classes/Objects


Python is an object oriented programming language.

Almost everything in Python is an object, with its properties(variables) and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.


Create a Class

To create a class, use the keyword class:


Example Create a class named MyClass, with a property named x:

class MyClass:
  x = 5

Create Object

Now we can use the class named MyClass to create objects:

Example Create an object named p1, and print the value of x:


p1 = MyClass()
print(p1.x)

The __init__() Function

The examples above are classes and objects in their simplest form, and are not really useful in real life applications.


To understand the meaning of classes we have to understand the built-in __init__() function.


All classes have a function called __init__(), which is always executed when the class is being initiated.


Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:


Example Create a class named Person, use the __init__() function to assign values for name and age:


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

p1 = Person("John", 36)

print(p1.name)
print(p1.age) 

>>John
>>36

Note: The__init__()function is called automatically every time the class is being used to create a new object.


Object Methods

Objects can also contain methods. Methods in objects are functions that belong to the object.


Methods/Functions= Behavior


Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program.


Let us create a method in the Person class:


Example Insert a function that prints a greeting, and execute it on the p1 object:

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

  def myfunc(self):
  print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

What is the difference between arguments and parameters in javascript or python?

Officially they are called parameters, but the actual arguments are given in the same called object. However, both words are interchangeable. Parameters are properties of a function. Arguments are properties of a particular call to a function.


Functions do specific things, classes are specific things.

Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.

Essentially, a class is a way of grouping functions (as methods) and data (as properties) into a logical unit revolving around a certain kind of thing. If you don't need that grouping, there's no need to make a class.


Variables = Properties?

Variables vs. Properties in JavaScript

What is a property? What is a variable? In what ways, if any, do they differ? Basic questions. Fundamental to understanding the language, but mostly overlooked in the literature of JavaScript. The VariableObject In order to understand what a JavaScript variable is we need to know about the VariableObject. In JavaScript, code can be executed within the global or functional context. There is only one global context. There is one functional context per function call. (There is also a third case – eval context which we’ll touch on later). Each execution context has an associated VariableObject. Variables (and functions) created within a given context exist as properties of that context’s VariableObject. The global context’s VariableObject is the global object. In a browser thats window:

var a = "hello"; //window is the global VariableObject window.a; //hello In the functional context its trickier. Each function context has a VariableObject (in this context known as an ActivationObject) but you can’t access it (unless you’re using Rhino). You just need to know that its there. Hence when you create a variable within a function context you can’t reference it as a property.


function foo() {
 var bar = "sausage";
 window.bar; //undefined (VariableObject is not window)
}

What is a property?


In other words, properties are the building blocks of objects.

//Examples of properties
foo.bar = "baz";
window.alert;
a.b = function(c,d,e) {return (c * d) + e};
Math.PI;
myArray[5];

What is a variable?

Lets try this: An association between a name and a value that exists within an execution context

Already we can see the essential difference emerging. Properties belong to objects; Variables belong to contexts (and context happens to have an object representation – the VariableObject).


//Examples of variables
var bar = 2; //global context
function foo = function() {
 var a; //function context
 f = 4; //global context (probably unintentionally)
}

But variables and properties are interchangeable right?

Not really, although it might appear that way:

//define as a property, access as a variable
window.foo = "a";
foo; //a
 
//define as a variable, access as a property
var bar = 54;
window.bar; //54

©2020 by Arturo Devesa.