Answers for "basic class python"

16

declare class python

# To create a simple class:
class Shape:
  	def __init__():
      	print("A new shape has been created!")
      	pass
    
    def get_area(self):
		pass

# To create a class that uses inheritance and polymorphism
# from another class:
class Rectangle(Shape):
  
	def __init__(self, height, width): # The constructor
    	super.__init__()
        self.height = height
    	self.width = width

	def get_area(self):
      	return self.height * self.width
Posted by: Guest on March-08-2020
4

python class

class Dog(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def speak(self):
        print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')

JUB0T = Dog('JUB0T', 55)
Friend = Dog('Doge', 10)
JUB0T.speak()
Friend.speak()
Posted by: Guest on November-20-2020
0

why should i use classes in python

"Why should I use classes in python?"

Classes are essential in OOP.
A good reason to use classes is to make your code structured and organised. 
Furthermore it is easy to import functions when they are part of a class since you can just import the class.
Classes provide methods that are useful such as the __repr__ method which allows you to represent an object by a string.
Classes allow for access control (encapsulation) by using the underscore character.
Posted by: Guest on July-19-2021

Python Answers by Framework

Browse Popular Code Answers by Language