Ansuman Prasad Pati
Ansuman Prasad Pati

Reputation: 43

When to create Class file and function file during Python programming?

I am beginner to Python. I am getting bit confuse while practicing program. Please help me, how can I determine that I need to create a class file and when I should go for a function file?

Upvotes: 1

Views: 764

Answers (2)

S.B
S.B

Reputation: 16496

I would briefly say that:

Classes are the smallest component in Object Oriented Programming, so use them whenever you want to benefit from OOP. I mean inheritance, encapsulation, polymorphism, abstraction...

Functions are helpful when you want to take repetitive code out of the main code and call the same block of code over and over with just changing the input.

  • You should ommit the word "file" from your question. There is no class file or function file. If you have a file that has some Python code inside, it is called a module. Classes and Functions are defined inside the module.

Upvotes: 0

Geetha Kanigiri
Geetha Kanigiri

Reputation: 64

Create a function. Functions do specific things, classes are specific things.

1.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.

2.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.

Upvotes: 2

Related Questions