Example:
```python
class Car:
def init(self, color):
self.color = color
my_car = Car("red")
your_car = Car("blue")
print(my_car.color) # Output: red
print(your_car.color) # Output: blue
```
- Methods: These are functions that belong to a class and operate on the instance variables of that class. They can be called by accessing the method name through an object instance.
Example:
```python
class Car:
def init(self, color):
self.color = color
def paint(self, new_color):
self.color = new_color
my_car = Car("red")
my_car.paint("blue")
print(my_car.color) # Output: blue
```
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def set_name(self, new_name):
self.name = new_name
def get_name(self):
return self.name
def set_age(self, new_age):
if new_age > 0:
self.age = new_age
def get_age(self):
return self.age
john = Person("John", 25)
print(f"{john.get_name()} is {john.get_age()} years old.")
What causes it: Attempting to access an instance variable or method that doesn't exist in the current scope or class.
class Car:
def __init__(self, color):
self.color = color
def paint(self, new_color):
self.color = new_color
my_car = Car("red")
print(my_car.unassigned_variable) # NameError: name 'unassigned_variable' is not defined
Solution: Define the instance variable or method in the class.
class Car:
def __init__(self, color):
self.color = color
self.num_wheels = 4
def paint(self, new_color):
self.color = new_color
Why it happens: The variable or method was not properly defined within the class's scope.
How to prevent it: Make sure all necessary instance variables and methods are defined in the class before trying to access them.
What causes it: Attempting to access an instance variable that doesn't exist for the current object or a method of a different class with the same name.
class Car:
def __init__(self, color):
self.color = color
class Truck:
pass
my_car = Car("red")
print(my_car.num_wheels) # AttributeError: 'Car' object has no attribute 'num_wheels'
Solution: Define the missing instance variable in the class or create a method that returns the desired value if it is calculated dynamically.
class Car:
def __init__(self, color):
self.color = color
def num_wheels(self):
return 4
my_car = Car("red")
print(my_car.num_wheels()) # Output: 4
Why it happens: The object instance does not have the specific instance variable or the method name is being used for a different class with the same name.
How to prevent it: Double-check that you are using the correct instance variable or method for the object and ensure there are no conflicting names among classes.
What causes it: Assigning an incompatible data type to an instance variable during initialization or modifying its value later on.
class Car:
def __init__(self, color):
self.color = color
my_car = Car("red")
my_car.num_wheels = "four" # TypeError: 'str' object does not support item assignment
Solution: Ensure that the data type assigned to the instance variable is compatible with its expected type.
class Car:
def __init__(self, color):
self.color = color
self.num_wheels = 4
my_car = Car("red")
my_car.num_wheels = 5 # This won't raise a TypeError since int and integer are compatible
Why it happens: The data type being assigned to the instance variable does not match its expected data type, such as assigning a string to an integer variable.
How to prevent it: Use appropriate data types for instance variables, and verify that any user input or external values passed to the class are compatible with the intended data type of the instance variable.