Answers for "class structure python"

2

class in python

class LuckyLemur():
  	def __init__(self, description):
		self.description = description
	
    def reveal_description(self):
    	print(f'Lucky Lemur is {self.description}')

lucky_lemur = LuckyLemur('Pro')
lucky_lemur.reveal_description()
Posted by: Guest on June-02-2021
1

python classes

class Student:
  def __init__(self, id, name, age):
    self.name = name
    self.id = id
    self.age = age
  
  def greet(self):
    print(f"Hello there.nMy name is {self.name}")
    
  def get_age(self):
    print(f"I am {self.age}")
    
  def __add__(self, other)
  	return Student(
      self.name+" "+other.name,
      self.id + " "+ other.id,
      str(self.age) +" "+str(other.age))
    
p1 = Student(1, "Jay", 19)
p2 = Student(2, "Jean", 22)
p3 = Student(3, "Shanna", 32)
p4 = Student(4, "Kayla", 23)


result = p1+p3
Posted by: Guest on February-16-2021
0

python class

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'
Posted by: Guest on May-19-2021
-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