0
why we use class in python is there any need
6 Answers
+ 2
yes. class specify module. actually in normal python use def . but if you want special module like scrapy or django.module you need classes
+ 2
actually module classes specify modules .
class mainview(django.templeteview)
class spider(scrapy.spider)
class builder(Gtk.builder)
in these if you remove the module class can't derive functions from that module so it will fail . here class is representing modules .
but when you write function as the class base ...like class new:
def def ...in that case it doesn't represent module as you will use that class as module .
+ 1
Yes there is a need : when you want to have related functions that manipulate kind of the same variables, it's better to use a class than use def a the top level.
Also, class DON'T specify module : you can have several classes in a single module
+ 1
really ? You are mistaking the inheritance mechanism with modules. Modules and inheritance work VERY differently :
when you inherit from a class, Python first checks if you define your method in the subclass, then (if it didn't find it) goes to the next class in line (not necessarily the parent, if you ask) and repeats with that class.
However, a module is just a namespace for your classes and functions. You can consider classes and functions as some kind of module, as in they provide a NAMESPACE for everything that is inside.
A class can't represent a module, nor can a module represent a class. A class is really used (in Python) to manipulate more easily related values and functions, not to BE a module.
For example, for my CS classes, I made a game purely based on classes, because they are easier to work with, and you can easily override the methods from object that you need. It's that easy. I don't want to know the length of my code if I had not used classes...
0
thanks all of u
0
Welcome!