HamHamJ
0
Q:

python classes

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("John", 36)
p1.myfunc() 
43
class MyClass(object):
  def __init__(self, x):
    self.x = x
11
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40
3
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 
14

class Box(object): #(object) ending not required
  def __init__(self, color, width, height): # Constructor: These parameters will be used upon class calling(Except self)
    self.color = color # self refers to global variables that can only be used throughout the class
    self.width = width
    self.height = height
    self.area = width * height
  def writeAboutBox(self): # self is almost always required for a function in a class, unless you don't want to use any of the global class variables
    print(f"I'm a box with the area of {self.area}, and a color of: {self.color}!")

greenSquare = Box("green", 10, 10) #Creates new square
greenSquare.writeAboutBox() # Calls writeAboutBox function of greenSquare object
7

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()
0
# Python program to 
# demonstrate instantiating 
# a class 
  
  
class Dog:  
      
    # A simple class 
    # attribute 
    attr1 = "mamal"
    attr2 = "dog"
  
    # A sample method   
    def fun(self):  
        print("I'm a", self.attr1) 
        print("I'm a", self.attr2) 
  
# Driver code 
# Object instantiation 
Rodger = Dog() 
  
# Accessing class attributes 
# and method through objects 
print(Rodger.attr1) 
Rodger.fun() 
3
class Animal(object): # Doesn't need params but put it there anyways.
    def __init__(self, species, price):
        self.species = species # Sets species name
        self.price = price # Sets price of it
    
    def overview(self): # A function that uses the params of the __init__ function
        print(f"This species is called a {self.species} and the price for it is {self.price}")

class Fish(Animal): # Inherits from Animal
    pass # Don't need to add anything because it's inherited everything from Animal
 
salmon = Fish("Salmon", "$20") # Make a object from class Fish
salmon.overview() # Run a function with it
dog = Animal("Golden retriever", "$400") # Make a object from class Animal
dog.overview() # Run a function with it
8
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)
1

  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("John", 
  36)
p1.myfunc() 
0

New to Communities?

Join the community