Be Cn
0
Q:

class in python

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 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
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the samne
thing. In addition variables that are stored inside a class are named 
attributes. The point of a class is to call the class later allowing you 
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
0
# Python CLASSMETHOD 
  
class Subject: 
      
    # create a variable 
    favorite_subject = "Networking"
      
    # create a function 
    def favorite_subject_name(obj): 
        print("My favorite_subject_name is : ", 
                           obj.favorite_subject) 
          
# create favorite_subject_name classmethod 
# before creating this line favorite_subject_name() 
# It can be called only with object not with class 
Subject.favorite_subject_name = classmethod(Subject.favorite_subject_name) 
  
# now this method can be called as classmethod 
# favorite_subject_name() method is called as class method 
Subject.favorite_subject_name() 
-1

New to Communities?

Join the community