Object
Let's learn about objects and classes in PowerShell and Python.
Object Introspection
PowerShell and Python are both object-oriented programming languages. This means that almost everything is implemented using a special programming construct called classes. These are used to create objects that have properties and functionalities, like any real-world object. Before we dig deep into this, let’s take a moment to understand the basics of classes, objects, and their members.
Class, Object, Property, and Method
Let’s start with a discussion of classes.
Class
Generally speaking, a class is a category, set, or classification of things that have some attribute in common and can be differentiated from other classes by kind, type, or quality. For example, Human
is a class that is different from the Plant
class. In terms of programming languages, especially Python and PowerShell, a class is a template for creating instances of the class known as objects.
The simplest way to create or define a class in Python and PowerShell is by using the class
keyword. For example, let’s create a simple class Human
with some properties and functionalities in Python. First, we have to use the class
keyword with the name of the class followed by a colon (:
). In the next line, we have to change the indentation and provide a body for the class, as demonstrated in the following example:
class Human:
# this is a property/attribute
name = 'homo sapiens'
height = 5
# 'def' is used to define a method/function of a class
def eat(self):
print(self.name,'is eating now')
PowerShell, on the other hand, has some small syntactic differences. The class body is enclosed in brackets { }
, although we use the def
keyword to define methods like in Python. Overall, there are a lot of similarities in how a basic class is defined in both scripting languages, as we saw from the previous and the following example of a class.
class Human {
# this is a property/attribute
[String]$name = 'homo sapiens'
[Int] $height = 5
# define a method/function of a class
eat(){
Write-Host $this.name 'is eating now'
}
}
Object
Objects are instances of a category (class) that have some attributes (properties) and perform some functions (methods). Just for the sake of simplicity, let’s suppose there is a category or a classification called Human
. Humans represent us as a species. If we take an instance of the class Human
, let’s call it John
, then John is an object of the class human. The John
object has all attributes and functionalities of the class Human
because all objects are created from the same class template.
Attributes or Properties
Attribute, or Property, is just another word for the characteristic of an object like:
name
height
weight
Functions or Methods
Functions are actions performed by the object and can sometimes be called methods in programming languages.
eat()
sleep()
talk()
Note: Functions and methods are often used interchangeably, which is incorrect. All methods are functions, but not all functions are methods.
A method is a function defined inside the body of the class. To access a method, we have to use the dot operator (.) on the object of the class. A function, on the other hand, can be standalone and is not necessarily in a class.
We’ll learn more about functions and methods in a later lesson.
Instantiating the Class
To create a Python object or an instance of a class, we can use the syntax <class name>()
and assign it to a variable for later use. It is good practice to start the name of the class with the first letter in uppercase.
## instance of a class also called an Object
john = Human()
PowerShell objects are created using the New-Object
cmdlet, followed by the name of the class.
$prateek = New-Object Human
## alternatively
$john = [Human]::new()
Accessing Attributes and Functions of an Object
In order to access the attributes and functions of an object, both PowerShell and Python use a Dot (.
) operator. In other words, to access the height
property or eat()
method of object John
in PowerShell, we would write something like this:
$john.height
$john.eat()
class Human {# this is a property/attribute[String]$name = 'homo sapiens'[Int] $height = 5# define a method/function of a classeat(){Write-Host $this.name 'is eating now'}}$prateek = New-Object Human$prateek.name="Prateek"Write-Host $prateek.name$prateek.height= 6Write-Host $prateek.height$prateek.eat()
A similar usage is employed in Python. We can instantiate a class then access and change the attributes using the dot operator or even call the methods of the class.
class Human:# this is a property/attributename = 'homo sapiens'height = 5# 'def' is used to define a method/function of a classdef eat(self):print(self.name,'is eating now')John=Human() # instantiating objectJohn.name="John" # Accesing name with '.' operatorprint(John.name) # Printing heightJohn.height=6 # Accessing height with '.' operatorprint(John.height) # Printing heightJohn.eat() # Calling eat method
Up to this point in the chapter, we have learned about classes, objects and how to access the members of an object. Now let’s look into some ways to inspect objects in PowerShell and Python, also known as object introspection. The purpose is to understand what the members of an object (.methods and properties) are.