Answers for "class python"

18

create and use python classes

class Mammal:
    def __init__(self, name):
        self.name = name

    def walk(self):
        print(self.name + " is going for a walk")


class Dog(Mammal):
    def bark(self):
        print("bark!")


class Cat(Mammal):
    def meow(self):
        print("meow!")


dog1 = Dog("Spot")
dog1.walk()
dog1.bark()
cat1 = Cat("Juniper")
cat1.walk()
cat1.meow()
Posted by: Guest on December-30-2019
12

class python

class MyClass(object):
  def __init__(self, x):
    self.x = x
Posted by: Guest on May-12-2020
1

class python

class Employee(Object)
	def __init__(self, name, age, salary):
    	self.name = name
      	self.age = age
        self.salary = salary
        
        
  
  	def __str__(self)
    return f"Employee {name} \nhes age {age} \nand make {salary}"
Posted by: Guest on July-20-2021
3

class python

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40
Posted by: Guest on October-29-2020
0

class python

class A:        # define your class A
.....

class B:         # define your class B
.....

class C(A, B):   # subclass of A and B
  
obj = C() #to create instance
# issubclass(sub, sup) boolean function returns true if the given 
# subclass sub is indeed a subclass of the superclass sup

# isinstance(obj, Class) boolean function returns true if obj is an 
# instance of class Class or is an instance of a subclass of Class
Posted by: Guest on November-24-2020
-1

class python

class Charge:
    def __init__(self, employee, discount):
        self.employee = employee
        self.discount = discount
        
    def _discounted_pricey(self):
        item_price = 100
        discounted_price = item_price * (1-self.discount)
        return discounted_price
        
    def __discounted_price(self):
        item_price = 100
        discounted_price = item_price * (1-self.discount)
        return discounted_price

    def pays(self):
        price_to_charge = self.__discounted_price() # gotcha: self.
        print(f'Charge {self.employee} ${price_to_charge:.2f} ({self.discount:.2%} off)')
    
Employee = Charge(employee='Billy Beans', discount=.1)

print(Employee.employee) # Billy Beans
print(Employee.discount) # 0.1
print(Employee._discounted_pricey()) # 90.0
print(Employee.__discounted_price()) # AttributeError: 'Charge' object has no attribute '__discounted_price'
print(Employee.item_price) # AttributeError: 'Charge' object has no attribute 'item_price'
Employee.pays() # Charge Billy Beans $90.00 (10.00% off)
Posted by: Guest on August-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language