Quiz Catalog

Catalog of quizzes

def foo():
  x = 1
  return x

foo.x = 4

print(foo(), foo.x)
1 4
  • scope
  • object/variable
  • function/variable
  • function/object

def f(a, x):
  return a.x + x

class Obj:
  def f(self, x):
    self.x = x
    return x + 2

obj = Obj()

print(f(obj, obj.f(2)))
6
  • object/variable

class A:
  def __init__(self, name):
    self.__name = name

  def greeting(self):
    print(self.__name)

class B(A):
  def __init__(self):
    pass
  
b = B("Anton")
b.greeting()
NameError: name 'Anton' is not defined
  • object/variable

class A:
  def __init__(self, name):
    __name = name

  def greeting(self):
    print(__name)

a = A("Anton")
a.greeting()
AttributeError: name '_A__name' is not defined
  • object/variable

class Soda:
  def __init__(self, ingredient):
    self.ingredient = ingredient if isinstance(ingredient, str) else None

  def show_my_drink(self):
    if self.ingredient:
      print(f'Soda and {self.ingredient}')
    else:
      print('Regular soda')

drink1 = Soda('raspberry')
drink2 = Soda(5)
print(drink1.show_my_drink(), drink2.show_my_drink())
Soda and raspberry Regular soda None None
  • object/variable