Objects A Sample Class


https://www.youtube.com/embed/FiABKEuaSJ8



code Sample

class Partyanimal:
    x = 0

    def party(self):
        self.x = self.x + 1
        print('So far', self.x)

an = Partyanimal()

an.party()
an.party()
an.party()

Output

So far 1
So far 2
So far 3

Code Sample

>>> x = list()
>>> type(x)
<class 'list'>
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>

COde Sample

class Partyanimal:
    x = 0

    def party(self):
        self.x = self.x + 1
        print('So far', self.x)

an = Partyanimal()

print('type', type(an))
print('Dir', dir(an))

Output

type <class '__main__.Partyanimal'>
Dir ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'party', 'x']
>>>


Question

  • What will the following program print?:

      class PartyAnimal:
          x = 0
          def party(self):
              self.x = self.x + 2
              print(self.x)
        
      an = PartyAnimal()
      an.party()
      an.party()
    
  • Ans

      2
      4
    

Notes mentioning this note


ນີ້ຄືກຣາຟທີ່ລິ້ງໄປຫາທຸກໜ້າໃນ garden ນີ້ 🕸️.