+ 2
Can any one explain me classes in python
26 Respuestas
+ 8
https://docs.python.org/3/tutorial/classes.html
This site can help you 👍
+ 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.
+ 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.
+ 2
dog=vertabrate()
dog.vertibra
will work and so will
dog=vertabrate()
dog.animal
+ 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
+ 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
+ 1
so our code is class Animalia():
pass
+ 1
for each class we can give the objects functions as apart of the class
+ 1
so for class animalia lets add a function that prints "im an animal
+ 1
class Animalia():
def animal():
print("I'm an animal")
+ 1
but currently we have no way to call this function so we add self as a peramiter to the function
+ 1
class Animalia():
def animal(self):
print("I'm an animal")
+ 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
+ 1
so lets add an object, lets call it dog
+ 1
we say that dog=Animalia()
+ 1
now lets write dog.animal, and when we run the code, it prints "I'm an animal")
+ 1
now, lets create a sub class called vertabrate
+ 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
+ 1
class vertabrate(Animalia):
pass
+ 1
lets create a function for vertabrate that says "I'm a vertabrate"