Classes - properties

8 important questions on Classes - properties


class ExampleClass:
    def __init__(self, val = 1):
        self.__first = val

    def set_second(self, val = 2):
        self.__second = val


example_object_1 = ExampleClass()

print(example_object_1.__dict__)

What is the output?

{'_ExampleClass__first': 1}


class ExampleClass:
    def __init__(self, val = 1):
        self.__first = val

    def set_second(self, val = 2):
        self.__second = val

example_object_1 = ExampleClass(2)

example_object_1.set_second(3)

print(example_object_1.__dict__)


What is the output?

{'_ExampleClass__first': 2, '_ExampleClass__second': 3}


class ExampleClass:
    def __init__(self, val = 1):
        self.__first = val

    def set_second(self, val = 2):
        self.__second = val


example_object_1 = ExampleClass(4)
example_object_1.__third = 5


print(example_object_1.__dict__)


What is the output?

{'_ExampleClass__first': 4, '__third': 5}
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

What is the value printed?


class ExampleClass:
    counter = 0
    def __init__(self, val = 1):
        self.__first = val
        ExampleClass.counter += 1


example_object_1 = ExampleClass()
example_object_2 = ExampleClass(2)
example_object_3 = ExampleClass(4)

print(example_object_1.counter)

* The value printed is 3
* The class variable "counter" increases by 1 for each object created.

Is a class variable shown in an objects dictionary?

print(example_object_1.__dict__)

No. Only instance variables are shown in an object's dictionary, not class variables.

Which of the Python class properties are instance variables and which are class variables? Which of them are private?



class Python:
    population = 1
    victims = 0
    def __init__(self):
        self.length_ft = 3
        self.__venomous = False

• Instance Variables:
1. length_ft
2. __venomous (private)

• Class Variables:
1. population
2. victims

You're going to negate the __venomous property of the version_2 object, ignoring the fact that the property is private. How will you do this?
version_2 = Python()

version_2._Python__venomous = not version_2._Python__venomous

Write an expression which checks if the version_2 object contains an instance property named constrictor (yes, constrictor!).

hasattr(version_2, 'constrictor')

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo