Answers for "how to make a class in python"

18

create and use python classes

class Mammal:
    def __init__(self, name):
        self.name = name

    def walk(self):
        print(self.name + " is going for a walk")


class Dog(Mammal):
    def bark(self):
        print("bark!")


class Cat(Mammal):
    def meow(self):
        print("meow!")


dog1 = Dog("Spot")
dog1.walk()
dog1.bark()
cat1 = Cat("Juniper")
cat1.walk()
cat1.meow()
Posted by: Guest on December-30-2019
21

how to make a class in python

class Person:
  def __init__(self, _name, _age):
    self.name = _name
    self.age = _age
   
  def sayHi(self):
    print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
    
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
Posted by: Guest on November-17-2019
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
5

python define class

class uneclasse():
  def __init__(self):
    pass
  def something(self):
    pass
xx = uneclasse()
xx.something()
Posted by: Guest on February-04-2020
19

python class

class Person:#set name of class to call it 
  def __init__(self, name, age):#func set ver
    self.name = name#set name
    self.age = age#set age
   

    def myfunc(self):#func inside of class 
      print("Hello my name is " + self.name)# code that the func dose

p1 = Person("barry", 50)# setting a ver fo rthe class 
p1.myfunc() #call the func and whitch ver you want it to be with
Posted by: Guest on June-12-2020
3

classes in python

class Fan:

    def __init__(self, company, color, number_of_wings):
        self.company = company
        self.color = color
        self.number_of_wings = number_of_wings
        
    def PrintDetails(self):
        print('This is the brand of',self.company,'its color is', self.color,' and it has',self.number_of_wings,'petals')
    
        
    def switch_on(self):
        print("fan started")
        
    def switch_off(self):
        print("fan stopped")
        
    def speed_up(self):
        print("speed increased by 1 unit")
        
    def speed_down(self):
        print("speed decreased by 1 unit")
        
usha_fan = Fan('usha','skin',5)
fan = Fan('bajaj','wite', 4)


print('these are the details of this fan')
usha_fan.PrintDetails()
print()

usha_fan.switch_on()
Posted by: Guest on June-28-2021

Code answers related to "how to make a class in python"

Python Answers by Framework

Browse Popular Code Answers by Language