python classes and objects properties code example

Example 1: class attributes in python

class MyClass:
  # Class attributes are defined outside of constructor
  class_attr = 0
  
  def __init__(self, inst):
    # Instance attributes are defined in the constructor
    self.instance_attr = inst
    
obj = MyClass(1)
print(obj.class_attr) # outputs 0
print(obj.instance_attr) # outputs 1
print(MyClass.class_attr) # outputs 0
print(MyClass.instance_attr) # raises AttributeError

Example 2: class and object in python

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

an=PartyAnimal()

an.Party()# this is same as 'PartyAnimal.Party(an)'

Example 3: Python Class Example

#Source: https://vegibit.com/python-class-examples/
class Vehicle:
    def __init__(self, brand, model, type):
        self.brand = brand
        self.model = model
        self.type = type
        self.gas_tank_size = 14
        self.fuel_level = 0

    def fuel_up(self):
        self.fuel_level = self.gas_tank_size
        print('Gas tank is now full.')

    def drive(self):
        print(f'The {self.model} is now driving.')

obj = Vehicle("Toyota", "Carola", "Car")
obj.drive()

Example 4: how to use class's in python

class person:
    name = "jake"
    age = 13
x = vars(person)
print(x)