How to assign types/values to variables in classes
Here, I'm trying to make an object (A 20 sided die) with the following qualities; Name, and Number of sides.
The way I wanna build this is that when I call the class (or make a new "Dice" object,) all I need to tell the code is what to name the dice, and how many sides there are on it. (1 to 20, in this case.)
class Dice: #The class itself, the outline for the Dice to come.
def __init__(self, name, nsides):
#The initializing bit, along with
#the stats I want these objects to carry.
self.name = name("")
self.nsides = nsides(list(range(int, int)))
#I was thinking the above code tells python that the value nsides,
#when referencing Dice, is a list, that is being organized into
#a range, where the items in said range would always be integers.
d20 = Dice("d20", (1, 21))
#This is where I'm creating an instance of Dice, followed by filling out
#the fields name and the integers in nsides's list/range.
print(d20.nsides)
print(d20.nsides)
#The expected outcome is the following being printed:
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
#What I get however is this error upon running:
#Expected type 'int'. got 'Type[int]' instead.
python list class range
add a comment |
Here, I'm trying to make an object (A 20 sided die) with the following qualities; Name, and Number of sides.
The way I wanna build this is that when I call the class (or make a new "Dice" object,) all I need to tell the code is what to name the dice, and how many sides there are on it. (1 to 20, in this case.)
class Dice: #The class itself, the outline for the Dice to come.
def __init__(self, name, nsides):
#The initializing bit, along with
#the stats I want these objects to carry.
self.name = name("")
self.nsides = nsides(list(range(int, int)))
#I was thinking the above code tells python that the value nsides,
#when referencing Dice, is a list, that is being organized into
#a range, where the items in said range would always be integers.
d20 = Dice("d20", (1, 21))
#This is where I'm creating an instance of Dice, followed by filling out
#the fields name and the integers in nsides's list/range.
print(d20.nsides)
print(d20.nsides)
#The expected outcome is the following being printed:
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
#What I get however is this error upon running:
#Expected type 'int'. got 'Type[int]' instead.
python list class range
1
self.nsides = [i for i in range(nsides[0], nsides[1])]
– pstatix
Jan 2 at 5:18
1
In your initialization, why do you callname()andnsides()? You've passed in the arguments, they are not pointers to functions.
– pstatix
Jan 2 at 5:19
So that's calling? I thought that was simply telling python that name = string and nsides = etc. I'm like fresh out the academy. In fact I've never even been to the academy. Is Stack Overflow like, not the place to be for learning from scratch? Like bullshit ain't tolerated here?
– mason villeneuve
Jan 2 at 5:23
Also could you possibly break down what each part of your code does/means?
– mason villeneuve
Jan 2 at 5:25
add a comment |
Here, I'm trying to make an object (A 20 sided die) with the following qualities; Name, and Number of sides.
The way I wanna build this is that when I call the class (or make a new "Dice" object,) all I need to tell the code is what to name the dice, and how many sides there are on it. (1 to 20, in this case.)
class Dice: #The class itself, the outline for the Dice to come.
def __init__(self, name, nsides):
#The initializing bit, along with
#the stats I want these objects to carry.
self.name = name("")
self.nsides = nsides(list(range(int, int)))
#I was thinking the above code tells python that the value nsides,
#when referencing Dice, is a list, that is being organized into
#a range, where the items in said range would always be integers.
d20 = Dice("d20", (1, 21))
#This is where I'm creating an instance of Dice, followed by filling out
#the fields name and the integers in nsides's list/range.
print(d20.nsides)
print(d20.nsides)
#The expected outcome is the following being printed:
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
#What I get however is this error upon running:
#Expected type 'int'. got 'Type[int]' instead.
python list class range
Here, I'm trying to make an object (A 20 sided die) with the following qualities; Name, and Number of sides.
The way I wanna build this is that when I call the class (or make a new "Dice" object,) all I need to tell the code is what to name the dice, and how many sides there are on it. (1 to 20, in this case.)
class Dice: #The class itself, the outline for the Dice to come.
def __init__(self, name, nsides):
#The initializing bit, along with
#the stats I want these objects to carry.
self.name = name("")
self.nsides = nsides(list(range(int, int)))
#I was thinking the above code tells python that the value nsides,
#when referencing Dice, is a list, that is being organized into
#a range, where the items in said range would always be integers.
d20 = Dice("d20", (1, 21))
#This is where I'm creating an instance of Dice, followed by filling out
#the fields name and the integers in nsides's list/range.
print(d20.nsides)
print(d20.nsides)
#The expected outcome is the following being printed:
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
#What I get however is this error upon running:
#Expected type 'int'. got 'Type[int]' instead.
python list class range
python list class range
asked Jan 2 at 5:14
mason villeneuvemason villeneuve
33
33
1
self.nsides = [i for i in range(nsides[0], nsides[1])]
– pstatix
Jan 2 at 5:18
1
In your initialization, why do you callname()andnsides()? You've passed in the arguments, they are not pointers to functions.
– pstatix
Jan 2 at 5:19
So that's calling? I thought that was simply telling python that name = string and nsides = etc. I'm like fresh out the academy. In fact I've never even been to the academy. Is Stack Overflow like, not the place to be for learning from scratch? Like bullshit ain't tolerated here?
– mason villeneuve
Jan 2 at 5:23
Also could you possibly break down what each part of your code does/means?
– mason villeneuve
Jan 2 at 5:25
add a comment |
1
self.nsides = [i for i in range(nsides[0], nsides[1])]
– pstatix
Jan 2 at 5:18
1
In your initialization, why do you callname()andnsides()? You've passed in the arguments, they are not pointers to functions.
– pstatix
Jan 2 at 5:19
So that's calling? I thought that was simply telling python that name = string and nsides = etc. I'm like fresh out the academy. In fact I've never even been to the academy. Is Stack Overflow like, not the place to be for learning from scratch? Like bullshit ain't tolerated here?
– mason villeneuve
Jan 2 at 5:23
Also could you possibly break down what each part of your code does/means?
– mason villeneuve
Jan 2 at 5:25
1
1
self.nsides = [i for i in range(nsides[0], nsides[1])]– pstatix
Jan 2 at 5:18
self.nsides = [i for i in range(nsides[0], nsides[1])]– pstatix
Jan 2 at 5:18
1
1
In your initialization, why do you call
name() and nsides()? You've passed in the arguments, they are not pointers to functions.– pstatix
Jan 2 at 5:19
In your initialization, why do you call
name() and nsides()? You've passed in the arguments, they are not pointers to functions.– pstatix
Jan 2 at 5:19
So that's calling? I thought that was simply telling python that name = string and nsides = etc. I'm like fresh out the academy. In fact I've never even been to the academy. Is Stack Overflow like, not the place to be for learning from scratch? Like bullshit ain't tolerated here?
– mason villeneuve
Jan 2 at 5:23
So that's calling? I thought that was simply telling python that name = string and nsides = etc. I'm like fresh out the academy. In fact I've never even been to the academy. Is Stack Overflow like, not the place to be for learning from scratch? Like bullshit ain't tolerated here?
– mason villeneuve
Jan 2 at 5:23
Also could you possibly break down what each part of your code does/means?
– mason villeneuve
Jan 2 at 5:25
Also could you possibly break down what each part of your code does/means?
– mason villeneuve
Jan 2 at 5:25
add a comment |
2 Answers
2
active
oldest
votes
Your error is occurring as you are parsing the 'range' class a new Int Class object, not an integer. However there are other errors, such as trying the call a string object: name("") which shouldn't work.
Your init class can be amended to:
def __init__(self, name, nsides):
self.name = name
self.nsides = list(range(*nsides))
The "*" will unpack your tuple into useable integers for the range class to use
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
add a comment |
class Dice: #The class itself, the outline for the Dice to come.
''' param:
name : str # name to given,
nsides : tuple/ list iterateable variable
return: None
'''
def __init__(self, name, nsides):
# passing the name (user input ) to class object self.name
self.name = name
# doing list comprehension
self.nsides = [ i for i in range(nsides[0],nsides[1])]
# range function works as range(a,b)-> [a,b) a is incluse and b is exclusive
# and it is incremented by 1 default
''' it is same as
self.nsides =
for i in range(nsides[0],nsides[1]):
self.nsides+=[i] # or self.nsides.append(i)
'''
d20 = Dice("d20", (1, 21))
print(d20.nsides)
# output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001521%2fhow-to-assign-types-values-to-variables-in-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your error is occurring as you are parsing the 'range' class a new Int Class object, not an integer. However there are other errors, such as trying the call a string object: name("") which shouldn't work.
Your init class can be amended to:
def __init__(self, name, nsides):
self.name = name
self.nsides = list(range(*nsides))
The "*" will unpack your tuple into useable integers for the range class to use
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
add a comment |
Your error is occurring as you are parsing the 'range' class a new Int Class object, not an integer. However there are other errors, such as trying the call a string object: name("") which shouldn't work.
Your init class can be amended to:
def __init__(self, name, nsides):
self.name = name
self.nsides = list(range(*nsides))
The "*" will unpack your tuple into useable integers for the range class to use
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
add a comment |
Your error is occurring as you are parsing the 'range' class a new Int Class object, not an integer. However there are other errors, such as trying the call a string object: name("") which shouldn't work.
Your init class can be amended to:
def __init__(self, name, nsides):
self.name = name
self.nsides = list(range(*nsides))
The "*" will unpack your tuple into useable integers for the range class to use
Your error is occurring as you are parsing the 'range' class a new Int Class object, not an integer. However there are other errors, such as trying the call a string object: name("") which shouldn't work.
Your init class can be amended to:
def __init__(self, name, nsides):
self.name = name
self.nsides = list(range(*nsides))
The "*" will unpack your tuple into useable integers for the range class to use
answered Jan 2 at 5:34
D HallD Hall
383
383
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
add a comment |
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
So I corrected my code, and it worked great! (Thanks by the way!) But just a quick question; why does "*" unpack the tuple. Also, why is the list variable being considered as a tuple? Are they the same thing? I read that one is "immutable" or something like that...
– mason villeneuve
Jan 2 at 5:44
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
An object that is surrounded in curly brackets ('spam', 'eggs') is a tuple. A list is defined in square brackets [ 'spam', 'eggs'], therefore you are sending the dice class a tuple, not a list. In this case, either would work. You are correct however that a tuple is immutable. With regards asterisk operator, check out information on *args and **kwargs. It will give you some useful insight into how python classes work.
– D Hall
Jan 2 at 6:00
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
Thanks my guyyyy! Cheers, wish me luck!
– mason villeneuve
Jan 2 at 6:07
add a comment |
class Dice: #The class itself, the outline for the Dice to come.
''' param:
name : str # name to given,
nsides : tuple/ list iterateable variable
return: None
'''
def __init__(self, name, nsides):
# passing the name (user input ) to class object self.name
self.name = name
# doing list comprehension
self.nsides = [ i for i in range(nsides[0],nsides[1])]
# range function works as range(a,b)-> [a,b) a is incluse and b is exclusive
# and it is incremented by 1 default
''' it is same as
self.nsides =
for i in range(nsides[0],nsides[1]):
self.nsides+=[i] # or self.nsides.append(i)
'''
d20 = Dice("d20", (1, 21))
print(d20.nsides)
# output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
add a comment |
class Dice: #The class itself, the outline for the Dice to come.
''' param:
name : str # name to given,
nsides : tuple/ list iterateable variable
return: None
'''
def __init__(self, name, nsides):
# passing the name (user input ) to class object self.name
self.name = name
# doing list comprehension
self.nsides = [ i for i in range(nsides[0],nsides[1])]
# range function works as range(a,b)-> [a,b) a is incluse and b is exclusive
# and it is incremented by 1 default
''' it is same as
self.nsides =
for i in range(nsides[0],nsides[1]):
self.nsides+=[i] # or self.nsides.append(i)
'''
d20 = Dice("d20", (1, 21))
print(d20.nsides)
# output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
add a comment |
class Dice: #The class itself, the outline for the Dice to come.
''' param:
name : str # name to given,
nsides : tuple/ list iterateable variable
return: None
'''
def __init__(self, name, nsides):
# passing the name (user input ) to class object self.name
self.name = name
# doing list comprehension
self.nsides = [ i for i in range(nsides[0],nsides[1])]
# range function works as range(a,b)-> [a,b) a is incluse and b is exclusive
# and it is incremented by 1 default
''' it is same as
self.nsides =
for i in range(nsides[0],nsides[1]):
self.nsides+=[i] # or self.nsides.append(i)
'''
d20 = Dice("d20", (1, 21))
print(d20.nsides)
# output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
class Dice: #The class itself, the outline for the Dice to come.
''' param:
name : str # name to given,
nsides : tuple/ list iterateable variable
return: None
'''
def __init__(self, name, nsides):
# passing the name (user input ) to class object self.name
self.name = name
# doing list comprehension
self.nsides = [ i for i in range(nsides[0],nsides[1])]
# range function works as range(a,b)-> [a,b) a is incluse and b is exclusive
# and it is incremented by 1 default
''' it is same as
self.nsides =
for i in range(nsides[0],nsides[1]):
self.nsides+=[i] # or self.nsides.append(i)
'''
d20 = Dice("d20", (1, 21))
print(d20.nsides)
# output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
answered Jan 2 at 5:32
prashant ranaprashant rana
938920
938920
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001521%2fhow-to-assign-types-values-to-variables-in-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown

1
self.nsides = [i for i in range(nsides[0], nsides[1])]– pstatix
Jan 2 at 5:18
1
In your initialization, why do you call
name()andnsides()? You've passed in the arguments, they are not pointers to functions.– pstatix
Jan 2 at 5:19
So that's calling? I thought that was simply telling python that name = string and nsides = etc. I'm like fresh out the academy. In fact I've never even been to the academy. Is Stack Overflow like, not the place to be for learning from scratch? Like bullshit ain't tolerated here?
– mason villeneuve
Jan 2 at 5:23
Also could you possibly break down what each part of your code does/means?
– mason villeneuve
Jan 2 at 5:25