Is it possible for objects of the same type/class to have different data attributes? [duplicate]
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?
python class
marked as duplicate by mkrieger1, timgeb
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.
add a comment |
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?
python class
marked as duplicate by mkrieger1, timgeb
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.
add a comment |
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?
python class
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
python class
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
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you for the quick answer. I was able to solve my problem.
– Fabio
Nov 17 '18 at 13:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Thank you for the quick answer. I was able to solve my problem.
– Fabio
Nov 17 '18 at 13:41
add a comment |
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
Thank you for the quick answer. I was able to solve my problem.
– Fabio
Nov 17 '18 at 13:41
add a comment |
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
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
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
add a comment |
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
add a comment |