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!
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 classes
class Student: def __init__(self, id, name, age): self.name = name self.id = id self.age = age def greet(self): print(f"Hello there.\nMy name is {self.name}") def get_age(self): print(f"I am {self.age}") def __add__(self, other) return Student( self.name+" "+other.name, self.id + " "+ other.id, str(self.age) +" "+str(other.age)) p1 = Student(1, "Jay", 19) p2 = Student(2, "Jean", 22) p3 = Student(3, "Shanna", 32) p4 = Student(4, "Kayla", 23) result = p1+p3
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
Source: pydoc.pages.taltech.ee
Defining a Class in Python
class MyNewClass: '''This is a docstring. I have created a new class''' pass
Python class example
class Greet: def __init__(self, names): self.names = names def say_hello(self): for name in self.names: print("Hello " + name)