<jupyter_start><jupyter_text>Classes in Python  Define a classA Python class can be defined using the `class` statement.<jupyter_code># Define class
class Car:
    """This is a car class. Described by docstring"""
    def start(self):
        """Define public class method"""
        return('Car has started')

# Create class object
bmw = Car()

# Call public class method
bmw.start()
# Retrieve class documentation
help(Car)
# Define class by extending or inheriting a parent class
class WheelCar(Car):
    # Define class variable shared by all instances of this class
    wheels = 4
    
ford = WheelCar()
anotherFord = WheelCar()
ford.wheels, anotherFord.wheels
# Define class with an initializer or constructor
class BrandCar(BrandCar):
    # Initialize with instance variable specific to each instance 
    def __init__(self, brand):
        self.brand = brand
    
    def about(self):
        return(self.brand + ' brand car has ' \
               + str(self.wheels) + ' wheels' )

porsche = BrandCar(brand='Porsche')
mustang = BrandCar(brand='Mustang')
porsche.about(), mustang.about()<jupyter_output><empty_output><jupyter_text>Attribute operationsUsing `hasattr`, `setattr`, `getattr`, and `del` functions you can perform operations on class methods, instance variables, and class variables.<jupyter_code># Methods, class and instance variables are attributes
hasattr(mustang, 'wheels'), hasattr(mustang, 'brand'), \
hasattr(mustang, 'about'), hasattr(mustang, 'start'), \
hasattr(mustang, 'engine')
# New instance variables can be created by assignment
mustang.engine = 'V8'
hasattr(mustang, 'engine'), hasattr(porsche, 'engine')
# Set and get attributes
setattr(mustang, 'brand', 'Mustang Plus')
getattr(mustang, 'brand')
# Instance variable reference can be deleted
del mustang.brand
hasattr(mustang, 'brand')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1079913606911447, 'fraction_numerical': 0.0010799136069114472, 'mean_word_length': 4.808777429467084, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '656824', 'license': 'MIT', 'n_tokens_mistral': 549, 'n_tokens_neox': 537, 'n_words': 185, 'path': 'python-classes.ipynb', 'repo_name': 'byte-bit/iris', 'size': '1852'}