Understanding Class, Objects

Hello Sir/Mam,

In the below code why do we define a function within the class and then pass a argument. Also, please make me learn on why it is important to create an object?
Also, why the Python interpretor doesn’t go sequentially on throwing outputs…?

class PartyAnimal:
x = 0
def party(self):
self.x = self.x + 1
print (“So far”, self.x)
an = PartyAnimal()

an.party()
an.party()
an.party()
print(“skip”)

so = PartyAnimal()

so.party()
an.party()
an.party()

#Problem: The Below Code isn’t working for me…

class PartyAnimal:
x = 0
name = “”
def init (self, nam):
self.name = nam
print (self.name,“Constructed”)

def party(self):
    self.x = self.x + 1
    print (self.name,"Party Count", self.x)

s = PartyAnimal(“Sally”)
s.party()

j = PartyAnimal(“Jim”)
j.party()
s.party()

Python is an object oriented programming language. Object-oriented programming is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields, and code, in the form of procedures. A feature of objects is an object’s procedures that can access and often modify the data fields of the object with which they are associated.

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. Objects can also contain methods. Methods in objects are functions that belong to the object. Here’s an example:

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()

As for your code, it would be very helpful if you would share a screenshot. Thanks.

1 Like