Home > Software design >  I am new to python & trying to understand the difference between class and object
I am new to python & trying to understand the difference between class and object

Time:03-07

Is it safe to say in this example: i=1, i is an object and class of this object is integer?

CodePudding user response:

You are completely right. Actually everything with some exceptions in Python is object. Functions and classes are objects. Language contructions (loops, condition instructions) and variables are not. For example:

def func():
    pass

print(func)

output:

<function func at 0x7fb278584280>

int is built-in class in Python.

print(int)

output

<class 'int'>

CodePudding user response:

You gonna have to make a more clear example to be able to answer your question in a more concise way. based on the example you made, you assigned integer 1 to the value i, they indicate value and variables, i is the value of 1. objects and classes aren't differentiated that way in python.

CodePudding user response:

  1. Class Class is a user-defined datatype that contain its own data members and member functions. The member functions and data members can be accessed with the help of objects. It is the primary concept of object-oriented programming. A class is used to organize information or data so a programmer can reuse the elements in multiple instances.

For example, if a programmer wants to make three instances of Dogs, Golden Retriever, poodle, and Maltese dog. The class dog would store similar information that is unique to each dog (although they are different species, they will have similar property attributes) and appropriate information will be associated with the class Dog.

  1. Object An object is an instance of a class. As already mentioned above, all the data members and member functions of the class can be accessed with the help of objects.

An object in OOP is a component which consists of properties to make a particular data useful. For example, let’s consider a class Student. We can access various student details using some common property attributes like student name, roll number, etc.

An object consists of –

Name – name of the variable Member data - data that describes the object Member functions - functions related to the object that also describes the behavior

I believe you get the answer

  • Related