python class constructor
class Human:
def __init__(self, name):
self.name = name
h1 = Human("Bob")
print(h1.name) # prints Bob, the name passed from constructor
python class constructor
class Human:
def __init__(self, name):
self.name = name
h1 = Human("Bob")
print(h1.name) # prints Bob, the name passed from constructor
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!
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
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
python class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name +".")
p1 = Person("Victor", 24)
p1.myfunc()
python class
class Dog:
def bark(self):
print("Woof!")
def roll(self):
print("*rolling*")
def greet(self):
print("Greetings, master")
def speak(self):
print("I cannot!")
# Creating the Dog class instance and saving it to the variable <clyde>
clyde = Dog()
clyde.bark() # --> Woof!
clyde.roll() # --> *rolling*
clyde.greet() # --> Greetings, master
clyde.speak() # --> I cannot!
# Creating another Dog instance
jenkins = Dog()
jenkins.bark() # --> Woof!
jenkins.roll() # --> *rolling*
# .. And other methods
# .. Infinite objects can be created this way, all implementing the same methods defined in our class
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us