+ 2

Can any one explain me classes in python

10th Jun 2021, 9:35 AM
Aarya chandel
27 Respostas
+ 8
https://docs.python.org/3/tutorial/classes.html This site can help you 👍
10th Jun 2021, 12:02 PM
aspad🇮🇷
aspad🇮🇷 - avatar
+ 6
Python is an object oriented programming language. 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.
10th Jun 2021, 11:59 AM
aspad🇮🇷
aspad🇮🇷 - avatar
+ 2
In order to understand what a class is, you should first understand what an object is. An 'object' is something that has its own set of properties and methods; it's somewhat like trying to make a new type. For example, if you consider all the humans as objects, then the 'properties' include the age, the number of limbs, the height, etc. while the 'methods' include acts like walking, typing talking, etc. A class is something that helps to construct such an object. Here's an example: class HomoSapiens(): def __init__(self, age, name, height): self.age = age self.name = name self.height = height def sayYourName(self): print(self.name) def ..... # etc. # Now, let's make an object with this class: Ian = HomoSapiens(20, "Ian Dane", 200cm) print(Ian.name) print(Ian.age) Ian.sayYourName() # the same as "print(Ian.name) As you can see, we've created an object called "Ian". The "HomoSapiens" class was used to make this object; it defines the behavior of the object.
11th Jun 2021, 4:29 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
dog=vertabrate() dog.vertibra will work and so will dog=vertabrate() dog.animal
11th Jun 2021, 10:16 PM
Avalon
+ 1
Think of it like taxonomy, where each level of specification has different attributes that are true for all members of that level of specification. In the case of python classes, the level's of spedification are the classes themselves, while the members of the specification are the objects you put into the class. Each object has its own unique values and attributes but they are all grouped together into classes
11th Jun 2021, 10:00 PM
Avalon
+ 1
the way that we define a class is by saying class and then whatever you want the class to be called, we will go with, animalia. followed by a semi-colon
11th Jun 2021, 10:03 PM
Avalon
+ 1
so our code is class Animalia(): pass
11th Jun 2021, 10:04 PM
Avalon
+ 1
for each class we can give the objects functions as apart of the class
11th Jun 2021, 10:04 PM
Avalon
+ 1
so for class animalia lets add a function that prints "im an animal
11th Jun 2021, 10:05 PM
Avalon
+ 1
class Animalia(): def animal(): print("I'm an animal")
11th Jun 2021, 10:05 PM
Avalon
+ 1
but currently we have no way to call this function so we add self as a peramiter to the function
11th Jun 2021, 10:06 PM
Avalon
+ 1
class Animalia(): def animal(self): print("I'm an animal")
11th Jun 2021, 10:07 PM
Avalon
+ 1
now that self is a peramiter(and in order for it to work, it has to be our first peramiter), we can call the function simply by adding ".animal" to the object in the class
11th Jun 2021, 10:08 PM
Avalon
+ 1
so lets add an object, lets call it dog
11th Jun 2021, 10:08 PM
Avalon
+ 1
we say that dog=Animalia()
11th Jun 2021, 10:09 PM
Avalon
+ 1
now lets write dog.animal, and when we run the code, it prints "I'm an animal")
11th Jun 2021, 10:10 PM
Avalon
+ 1
now, lets create a sub class called vertabrate
11th Jun 2021, 10:11 PM
Avalon
+ 1
to say that vertabrate is a sub class of animalia we put the exact name of the parent class in the parenthasis like this
11th Jun 2021, 10:11 PM
Avalon
+ 1
class vertabrate(Animalia): pass
11th Jun 2021, 10:12 PM
Avalon
+ 1
lets create a function for vertabrate that says "I'm a vertabrate"
11th Jun 2021, 10:12 PM
Avalon