Is it possible for objects of the same type/class to have different data attributes? [duplicate]












0
















This question already has an answer here:




  • One int for every python object [duplicate]

    3 answers




Sorry, if the title seemed a little imprecise.



I'm wondering if there is way of making sure that every instance of an object can have a unique serial number?



class Airplane:
def __init__(self, name, passenger_hold):
self.name = name
self.passenger_hold = passenger_hold

airplane1 = Airplane("Airbus A320", 100)
airplane2 = Airplane("Boeing 747", 250)


How can I make sure that the first airplane has the serial number 0, the second one 1 and so on?










share|improve this question















marked as duplicate by mkrieger1, timgeb python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 12:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • One int for every python object [duplicate]

      3 answers




    Sorry, if the title seemed a little imprecise.



    I'm wondering if there is way of making sure that every instance of an object can have a unique serial number?



    class Airplane:
    def __init__(self, name, passenger_hold):
    self.name = name
    self.passenger_hold = passenger_hold

    airplane1 = Airplane("Airbus A320", 100)
    airplane2 = Airplane("Boeing 747", 250)


    How can I make sure that the first airplane has the serial number 0, the second one 1 and so on?










    share|improve this question















    marked as duplicate by mkrieger1, timgeb python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 17 '18 at 12:38


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • One int for every python object [duplicate]

        3 answers




      Sorry, if the title seemed a little imprecise.



      I'm wondering if there is way of making sure that every instance of an object can have a unique serial number?



      class Airplane:
      def __init__(self, name, passenger_hold):
      self.name = name
      self.passenger_hold = passenger_hold

      airplane1 = Airplane("Airbus A320", 100)
      airplane2 = Airplane("Boeing 747", 250)


      How can I make sure that the first airplane has the serial number 0, the second one 1 and so on?










      share|improve this question

















      This question already has an answer here:




      • One int for every python object [duplicate]

        3 answers




      Sorry, if the title seemed a little imprecise.



      I'm wondering if there is way of making sure that every instance of an object can have a unique serial number?



      class Airplane:
      def __init__(self, name, passenger_hold):
      self.name = name
      self.passenger_hold = passenger_hold

      airplane1 = Airplane("Airbus A320", 100)
      airplane2 = Airplane("Boeing 747", 250)


      How can I make sure that the first airplane has the serial number 0, the second one 1 and so on?





      This question already has an answer here:




      • One int for every python object [duplicate]

        3 answers








      python class






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 13:43









      Ayxan

      1,583316




      1,583316










      asked Nov 17 '18 at 12:32









      FabioFabio

      33




      33




      marked as duplicate by mkrieger1, timgeb python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 17 '18 at 12:38


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by mkrieger1, timgeb python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 17 '18 at 12:38


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use a global counter, internal to your class that would give you a new value each time the constructor is called.



          class Airplane:
          counter = 0

          def __init__(self, name, passenger_hold):
          self.name = name
          self.passenger_hold = passenger_hold
          self.serial = Airplane.counter
          Airplane.counter += 1





          share|improve this answer


























          • Thank you for the quick answer. I was able to solve my problem.

            – Fabio
            Nov 17 '18 at 13:41


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use a global counter, internal to your class that would give you a new value each time the constructor is called.



          class Airplane:
          counter = 0

          def __init__(self, name, passenger_hold):
          self.name = name
          self.passenger_hold = passenger_hold
          self.serial = Airplane.counter
          Airplane.counter += 1





          share|improve this answer


























          • Thank you for the quick answer. I was able to solve my problem.

            – Fabio
            Nov 17 '18 at 13:41
















          1














          Use a global counter, internal to your class that would give you a new value each time the constructor is called.



          class Airplane:
          counter = 0

          def __init__(self, name, passenger_hold):
          self.name = name
          self.passenger_hold = passenger_hold
          self.serial = Airplane.counter
          Airplane.counter += 1





          share|improve this answer


























          • Thank you for the quick answer. I was able to solve my problem.

            – Fabio
            Nov 17 '18 at 13:41














          1












          1








          1







          Use a global counter, internal to your class that would give you a new value each time the constructor is called.



          class Airplane:
          counter = 0

          def __init__(self, name, passenger_hold):
          self.name = name
          self.passenger_hold = passenger_hold
          self.serial = Airplane.counter
          Airplane.counter += 1





          share|improve this answer















          Use a global counter, internal to your class that would give you a new value each time the constructor is called.



          class Airplane:
          counter = 0

          def __init__(self, name, passenger_hold):
          self.name = name
          self.passenger_hold = passenger_hold
          self.serial = Airplane.counter
          Airplane.counter += 1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '18 at 12:39









          AkshayNevrekar

          4,13491735




          4,13491735










          answered Nov 17 '18 at 12:37









          Matthieu BrucherMatthieu Brucher

          15.3k32140




          15.3k32140













          • Thank you for the quick answer. I was able to solve my problem.

            – Fabio
            Nov 17 '18 at 13:41



















          • Thank you for the quick answer. I was able to solve my problem.

            – Fabio
            Nov 17 '18 at 13:41

















          Thank you for the quick answer. I was able to solve my problem.

          – Fabio
          Nov 17 '18 at 13:41





          Thank you for the quick answer. I was able to solve my problem.

          – Fabio
          Nov 17 '18 at 13:41



          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini