CLASSES and OBJECTS



Python - Classes and Objects


Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities. Class. A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.


Python Classes/Objects :


Python is an object oriented programming language.

Almost everything in Python is an object, with its properties 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 :


class Myclass:
x=9
print(Myclass)

=====o/p====

<class '_main_.Myclass'>

Create Object :


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


class Myclass:
  x="freedom"
p1=Myclass()
print(p1.x)

====o/p====

freedom

The __init__() Function :


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

x = Person("Jay", 36)

print(x.name)
print(x.age)


======o/p======
Jay
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.

Let us create a method in the Person class:


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

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

y = Person("Johanes", 36)
y.myfunc()

=====o/p======

Hello my name is Johanes





Note: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.


The self Parameter :


The 'self' parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class:


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

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

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


======o/p=====

Hello my name is Janush



Another program of same type:

class person:
    def _init_(freedom,name,age):
      freedom.name = name
      freedom.age = age
        
    def myfunc(y):
      print("hello my name is" + y.name)
      
x = person("Nikil",56)
x.myfunc()

=====o/p=====

hello my name isNikil


Modify Object Properties :


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("rehman",36)
p1.age = 40
print(p1.age)


=====o/p=====

40


Delete Object Properties :


You can delete properties on objects by using the 'del' keyword :


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("rehman",36)
del p1.age 
print(p1.age)


======o/p=====

Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\DATATYPES.py", line 10, in <module>
    print(p1.age)
AttributeError: 'person' object has no attribute 'age'


Delete Objects :


You can delete objects by using the 'del' keyword:


Delete 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("Joy", 56)

del p1

print(p1)

======o/p=====

Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\DATATYPES.py", line 10, in <module>
    print(p1)
NameError: name 'p1' is not defined




The pass Statement :


class definitions cannot be empty, but if we have a class definition with no content for some reason, put in the pass statement to avoid getting an error.


class Person:
  pass

# having an empty class definition like this, would raise an error without the pass statement



RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials