What to Serve for Class in Python

8

I'm new to programming but there's one thing I do not understand. What is the class in Python?

Because it seems to me that with function I can do everything without using class . I already ran the net, but I did not find anything. Can anyone help me understand what class function in Python with some basic example?

    
asked by anonymous 20.02.2015 / 20:51

2 answers

14

The function of the classes is to unite structure with behavior in a logical way:

  • Lists, dictionaries, tuples, etc. describe a data structure . They can be more or less semantic, but they are sufficient to group common data. But the data itself does nothing, they just exist ...
  • Functions, procedures, lambdas, etc. describe a functionality , that is something to do, something that changes the internal state of the computer. They can receive data as input, return data as output, have side effects on other data, access streams ... However, each function is an isolated procedure, uncoupled from the individual data in which it operates. not global data).

Classes unite both in a single concept - the object - which at the same time has semantically organized data and also methods that act on that data in an organized way. This allows you to treat the class as a type , just like the existing types in the language (a number can be added, subtracted, multiplied, split, a string can be concatenated, C can undergo operations X , Y and Z ).

In the same way that a class can define a type, another class can define a subtype (ie a more specific type, a subset so to speak), reusing the structure and functionality of the superclass (the more general type) and adding its own.

There are several other details in the class concept, some applicable to Python and some not (eg Python has no encapsulation), but the basics are these. It is perfectly possible to program without using classes [defined by you], and in some cases simpler it is even recommendable, in my opinion (like everything else, classes are tools, to be used when they are useful and left out when they are not). But knowing them and knowing how to recognize them when using them will bring benefits, whether in clarity, conciseness, semantics, etc., will give you one more resource to deal with the inherent complexity of computational systems.

    
20.02.2015 / 22:06
0

In Python, the class statement defines a class

In Python, classes are basically dictionaries that can also contain functions and allow such functions to match the references of dictionary elements.

Now speaking more broadly, class is a concept of a programming paradigm called object orientation .

I will not invent the wheel. There are good explanations about object orientation out there:

02.02.2018 / 02:20