In this code snippet, the output will be an Exception. Here’s why:
1. The TV class is defined with no attributes or methods (pass is used as a placeholder).
2. An instance of the TV class, obj, is created.
3. A new attribute price is assigned to obj with the value 200. This attribute is dynamically added to the instance obj but is not part of the TV class itself.
4. The code then attempts to print self.price. However, self is not defined in the current scope. In Python, self is a conventionally used parameter name that refers to the instance of the class within a method of the class. Since this code tries to access self outside of a class method, it will raise a NameError for the undefined variable self.
To fix this and print the price, you would need to use obj.price instead:
print(obj.price)
This would correctly print 200.
0 Comments:
Post a Comment