Answers for "python declare object"

22

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!
Posted by: Guest on November-17-2019
16

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
Posted by: Guest on March-08-2020
4

class and object in python

#NOTES
class PartyAnimal:
  	def Party():
      #blahblah

an=PartyAnimal()

an.Party()# this is same as 'PartyAnimal.Party(an)'
Posted by: Guest on July-13-2020
0

how to create an object in python

class ClassName:
    self.attribute_1 = variable_1 #Set attributes for all object instances
    self.attrubute_2 = variable_2
    
    def __init__(self, attribute_3, attribute_4): #Set attributes at object creation
        self.attribute_3 = attribute_3            
        self.attribute_4 = attribute_4

    def method(self): #All methods should include self
		print("This is a method example.") #Define methods just like functions 


object = Object(4, "string") #Set attribute_3 and attribute_4
object.method() #Methods are called like this.
Posted by: Guest on August-09-2020
-1

how to create class in python

x.counter = 1
while x.counter < 10:
    x.counter = x.counter * 2
print(x.counter)
del x.counter
Posted by: Guest on June-18-2021

Python Answers by Framework

Browse Popular Code Answers by Language