Answers for "class and methods in python"

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
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

Code answers related to "class and methods in python"

Python Answers by Framework

Browse Popular Code Answers by Language