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 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()
classes in python
# Python classes
class Person():
# Class object attributes (attributes that not needed to be mentioned when creating new class of person)
alive = True
def __init__(self, name, age):
# In the __init__ method you can make attributes that will be mentioned when creating new class of person
self.name = name
self.age = age
def speak(self):
# In every method in class there will be self, and then other things (name, age, etc.)
print(f'Hello, my name is {self.name} and my age is {self.age}') # f'' is type of strings that let you use variable within the string
person_one = Person('Sam', 23) # Sam is the name attribute, and 23 is the age attribute
person_one.speak() # Prints Hello, my name is Sam and my age is 23
==================================================================
# Output:
>>> 'Hello, my name is Sam and my age is 23'
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