How to add individual number for every instance of a subclass? [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
Giving unique IDs to all nodes?
4 answers
I want to add an individual number (starting at 0) to every new instance of a subclass. I tried to create a class variable and add +1 every time a new instance is created. But it doesn't work how I would like:
class Subclass(Class):
number = -1
def __init__(self, argument1, argument2):
super().__init__(argument1, argument2)
self.number += 1
def get_number(self):
return self.number
What do I have to change? So that:
instance = Subclass(argument1, argument2)
instance2 = Subclass(argument1, argument2)
assert instance.get_number() == 0
assert instance2.get_number() == 1
python python-3.x
marked as duplicate by 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 19 at 17:05
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 |
up vote
0
down vote
favorite
This question already has an answer here:
Giving unique IDs to all nodes?
4 answers
I want to add an individual number (starting at 0) to every new instance of a subclass. I tried to create a class variable and add +1 every time a new instance is created. But it doesn't work how I would like:
class Subclass(Class):
number = -1
def __init__(self, argument1, argument2):
super().__init__(argument1, argument2)
self.number += 1
def get_number(self):
return self.number
What do I have to change? So that:
instance = Subclass(argument1, argument2)
instance2 = Subclass(argument1, argument2)
assert instance.get_number() == 0
assert instance2.get_number() == 1
python python-3.x
marked as duplicate by 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 19 at 17:05
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.
Do you need to know which the next number would be? If not you could useitertools.countthen something likeself.number = next(self.numbers).
– jonrsharpe
Nov 19 at 17:05
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Giving unique IDs to all nodes?
4 answers
I want to add an individual number (starting at 0) to every new instance of a subclass. I tried to create a class variable and add +1 every time a new instance is created. But it doesn't work how I would like:
class Subclass(Class):
number = -1
def __init__(self, argument1, argument2):
super().__init__(argument1, argument2)
self.number += 1
def get_number(self):
return self.number
What do I have to change? So that:
instance = Subclass(argument1, argument2)
instance2 = Subclass(argument1, argument2)
assert instance.get_number() == 0
assert instance2.get_number() == 1
python python-3.x
This question already has an answer here:
Giving unique IDs to all nodes?
4 answers
I want to add an individual number (starting at 0) to every new instance of a subclass. I tried to create a class variable and add +1 every time a new instance is created. But it doesn't work how I would like:
class Subclass(Class):
number = -1
def __init__(self, argument1, argument2):
super().__init__(argument1, argument2)
self.number += 1
def get_number(self):
return self.number
What do I have to change? So that:
instance = Subclass(argument1, argument2)
instance2 = Subclass(argument1, argument2)
assert instance.get_number() == 0
assert instance2.get_number() == 1
This question already has an answer here:
Giving unique IDs to all nodes?
4 answers
python python-3.x
python python-3.x
edited Nov 19 at 17:04
Micha Wiedenmann
9,8661164102
9,8661164102
asked Nov 19 at 17:03
comadenem
1
1
marked as duplicate by 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 19 at 17:05
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 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 19 at 17:05
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.
Do you need to know which the next number would be? If not you could useitertools.countthen something likeself.number = next(self.numbers).
– jonrsharpe
Nov 19 at 17:05
add a comment |
Do you need to know which the next number would be? If not you could useitertools.countthen something likeself.number = next(self.numbers).
– jonrsharpe
Nov 19 at 17:05
Do you need to know which the next number would be? If not you could use
itertools.count then something like self.number = next(self.numbers).– jonrsharpe
Nov 19 at 17:05
Do you need to know which the next number would be? If not you could use
itertools.count then something like self.number = next(self.numbers).– jonrsharpe
Nov 19 at 17:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You need to refer to the class field number using class object, not self:
class Counted():
created = 0
def __init__(self):
self.ordinal = Counted.created
Counted.created += 1
assert Counted().ordinal == 0
assert Counted().ordinal == 1
assert Counted().ordinal == 2
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You need to refer to the class field number using class object, not self:
class Counted():
created = 0
def __init__(self):
self.ordinal = Counted.created
Counted.created += 1
assert Counted().ordinal == 0
assert Counted().ordinal == 1
assert Counted().ordinal == 2
add a comment |
up vote
0
down vote
You need to refer to the class field number using class object, not self:
class Counted():
created = 0
def __init__(self):
self.ordinal = Counted.created
Counted.created += 1
assert Counted().ordinal == 0
assert Counted().ordinal == 1
assert Counted().ordinal == 2
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to refer to the class field number using class object, not self:
class Counted():
created = 0
def __init__(self):
self.ordinal = Counted.created
Counted.created += 1
assert Counted().ordinal == 0
assert Counted().ordinal == 1
assert Counted().ordinal == 2
You need to refer to the class field number using class object, not self:
class Counted():
created = 0
def __init__(self):
self.ordinal = Counted.created
Counted.created += 1
assert Counted().ordinal == 0
assert Counted().ordinal == 1
assert Counted().ordinal == 2
answered Nov 19 at 17:09
sds
38.3k1492166
38.3k1492166
add a comment |
add a comment |
Do you need to know which the next number would be? If not you could use
itertools.countthen something likeself.number = next(self.numbers).– jonrsharpe
Nov 19 at 17:05