Meaning of * operator when used with classes
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've been trying to follow a video tutorial on Python and cannot understand one operation the developer performs.
class Polynomial():
def __init__(self, *coeffs):
self.coeffs = coeffs # (3,4,3)
def __repr__(self):
return 'Polynomial(*!r)'.format(self.coeffs)
def __add__(self, other):
print(self.coeffs)
print(other.coeffs)
z = (x + y for x, y in zip(self.coeffs, other.coeffs))
print(z)
return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
p1 = Polynomial(1, 2, 3) # x^2 + 2x + 3
p2 = Polynomial(3, 4, 3) # 3x^2 + 4x + 3
#print(p2) # Polynomial(*(3, 4, 3))
print(p1 + p2) # Polynomial(*(4, 6, 6))
The above example will print
<generator object Polynomial.__add__.<locals>.<genexpr> at 0x030D0390>
as the return value of z, I cannot understand why because I am performing a zip operation of two tuples?
Alongside that problem, I do not understand why removing the *
during the return of __add__
causes a problem i.e return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
to return Polynomial((x + y for x, y in zip(self.coeffs, other.coeffs)))
What is the * operator doing, and why is z an object of Polynomial?
The _add__
method does not contain a parameter containing a *
or **
and is therefore a different situation.
python
add a comment |
I've been trying to follow a video tutorial on Python and cannot understand one operation the developer performs.
class Polynomial():
def __init__(self, *coeffs):
self.coeffs = coeffs # (3,4,3)
def __repr__(self):
return 'Polynomial(*!r)'.format(self.coeffs)
def __add__(self, other):
print(self.coeffs)
print(other.coeffs)
z = (x + y for x, y in zip(self.coeffs, other.coeffs))
print(z)
return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
p1 = Polynomial(1, 2, 3) # x^2 + 2x + 3
p2 = Polynomial(3, 4, 3) # 3x^2 + 4x + 3
#print(p2) # Polynomial(*(3, 4, 3))
print(p1 + p2) # Polynomial(*(4, 6, 6))
The above example will print
<generator object Polynomial.__add__.<locals>.<genexpr> at 0x030D0390>
as the return value of z, I cannot understand why because I am performing a zip operation of two tuples?
Alongside that problem, I do not understand why removing the *
during the return of __add__
causes a problem i.e return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
to return Polynomial((x + y for x, y in zip(self.coeffs, other.coeffs)))
What is the * operator doing, and why is z an object of Polynomial?
The _add__
method does not contain a parameter containing a *
or **
and is therefore a different situation.
python
2
Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
– Norrius
Nov 15 '18 at 9:19
add a comment |
I've been trying to follow a video tutorial on Python and cannot understand one operation the developer performs.
class Polynomial():
def __init__(self, *coeffs):
self.coeffs = coeffs # (3,4,3)
def __repr__(self):
return 'Polynomial(*!r)'.format(self.coeffs)
def __add__(self, other):
print(self.coeffs)
print(other.coeffs)
z = (x + y for x, y in zip(self.coeffs, other.coeffs))
print(z)
return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
p1 = Polynomial(1, 2, 3) # x^2 + 2x + 3
p2 = Polynomial(3, 4, 3) # 3x^2 + 4x + 3
#print(p2) # Polynomial(*(3, 4, 3))
print(p1 + p2) # Polynomial(*(4, 6, 6))
The above example will print
<generator object Polynomial.__add__.<locals>.<genexpr> at 0x030D0390>
as the return value of z, I cannot understand why because I am performing a zip operation of two tuples?
Alongside that problem, I do not understand why removing the *
during the return of __add__
causes a problem i.e return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
to return Polynomial((x + y for x, y in zip(self.coeffs, other.coeffs)))
What is the * operator doing, and why is z an object of Polynomial?
The _add__
method does not contain a parameter containing a *
or **
and is therefore a different situation.
python
I've been trying to follow a video tutorial on Python and cannot understand one operation the developer performs.
class Polynomial():
def __init__(self, *coeffs):
self.coeffs = coeffs # (3,4,3)
def __repr__(self):
return 'Polynomial(*!r)'.format(self.coeffs)
def __add__(self, other):
print(self.coeffs)
print(other.coeffs)
z = (x + y for x, y in zip(self.coeffs, other.coeffs))
print(z)
return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
p1 = Polynomial(1, 2, 3) # x^2 + 2x + 3
p2 = Polynomial(3, 4, 3) # 3x^2 + 4x + 3
#print(p2) # Polynomial(*(3, 4, 3))
print(p1 + p2) # Polynomial(*(4, 6, 6))
The above example will print
<generator object Polynomial.__add__.<locals>.<genexpr> at 0x030D0390>
as the return value of z, I cannot understand why because I am performing a zip operation of two tuples?
Alongside that problem, I do not understand why removing the *
during the return of __add__
causes a problem i.e return Polynomial(*(x + y for x, y in zip(self.coeffs, other.coeffs)))
to return Polynomial((x + y for x, y in zip(self.coeffs, other.coeffs)))
What is the * operator doing, and why is z an object of Polynomial?
The _add__
method does not contain a parameter containing a *
or **
and is therefore a different situation.
python
python
edited Nov 15 '18 at 9:21
J.doe
asked Nov 15 '18 at 9:13
J.doeJ.doe
233
233
2
Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
– Norrius
Nov 15 '18 at 9:19
add a comment |
2
Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
– Norrius
Nov 15 '18 at 9:19
2
2
Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
– Norrius
Nov 15 '18 at 9:19
Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
– Norrius
Nov 15 '18 at 9:19
add a comment |
3 Answers
3
active
oldest
votes
So first.
Your print is ok. You defined a generator using () parentheses. You can change this to and then you should see the elements in list.
Or you can use your generator, so print:
print([el for el in z])
Second, the *.
It will simply pass iterable as separated args, so:
SomeClass(*args)
Will do:
SomeClass(args[0], args[1], args[2], ...)
You can read about this in official docs (single asterisk), here: https://docs.python.org/3/reference/expressions.html#expression-lists
And here (double asterisk):
https://docs.python.org/3/reference/expressions.html#dictionary-displays
1
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
add a comment |
When in a function declaration, * is list of args send.
For exemple:
def my_function(a, *b, **c):
print(a)
print(b)
print(c)
my_function("1st arg", "other arg", "other arg again", 2, arg_type="kwargs")
output :
1st args
["other arg", "other arg again", 2]
"arg_type": "kwargs"
And when is not in function declaration, it's for unpack the list.
For exemple:
list_of_arguments = ['a', 'b', 'z']
my_str = "the first letter of alphabet is , the second is and the last is "
print(my_str.format(*list_of_arguments))
or other exemple
def my_second_func(a, b, c):
print(a)
print(b)
my_list = [1, 2, 3]
my_second_func(a, b, c)
will output:
1
2
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
i edited my post, in your code*
is used for unpack the list returned by the zip() function
– iElden
Nov 15 '18 at 9:22
add a comment |
Consider this easy example:
a = [1, 2, 3]
b = [4, 5, 6]
gen = (x + y for x, y in zip(a, b))
print(gen) ## this will print <generator object <genexpr> at 0x...>
In this case, the asterisk evaluates the generator expression. So when doing
print(*gen) ## this will print 5 7 9
you evaluate the generator expression.
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%2f53315930%2fmeaning-of-operator-when-used-with-classes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
So first.
Your print is ok. You defined a generator using () parentheses. You can change this to and then you should see the elements in list.
Or you can use your generator, so print:
print([el for el in z])
Second, the *.
It will simply pass iterable as separated args, so:
SomeClass(*args)
Will do:
SomeClass(args[0], args[1], args[2], ...)
You can read about this in official docs (single asterisk), here: https://docs.python.org/3/reference/expressions.html#expression-lists
And here (double asterisk):
https://docs.python.org/3/reference/expressions.html#dictionary-displays
1
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
add a comment |
So first.
Your print is ok. You defined a generator using () parentheses. You can change this to and then you should see the elements in list.
Or you can use your generator, so print:
print([el for el in z])
Second, the *.
It will simply pass iterable as separated args, so:
SomeClass(*args)
Will do:
SomeClass(args[0], args[1], args[2], ...)
You can read about this in official docs (single asterisk), here: https://docs.python.org/3/reference/expressions.html#expression-lists
And here (double asterisk):
https://docs.python.org/3/reference/expressions.html#dictionary-displays
1
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
add a comment |
So first.
Your print is ok. You defined a generator using () parentheses. You can change this to and then you should see the elements in list.
Or you can use your generator, so print:
print([el for el in z])
Second, the *.
It will simply pass iterable as separated args, so:
SomeClass(*args)
Will do:
SomeClass(args[0], args[1], args[2], ...)
You can read about this in official docs (single asterisk), here: https://docs.python.org/3/reference/expressions.html#expression-lists
And here (double asterisk):
https://docs.python.org/3/reference/expressions.html#dictionary-displays
So first.
Your print is ok. You defined a generator using () parentheses. You can change this to and then you should see the elements in list.
Or you can use your generator, so print:
print([el for el in z])
Second, the *.
It will simply pass iterable as separated args, so:
SomeClass(*args)
Will do:
SomeClass(args[0], args[1], args[2], ...)
You can read about this in official docs (single asterisk), here: https://docs.python.org/3/reference/expressions.html#expression-lists
And here (double asterisk):
https://docs.python.org/3/reference/expressions.html#dictionary-displays
answered Nov 15 '18 at 9:21
opalczynskiopalczynski
1,045811
1,045811
1
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
add a comment |
1
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
1
1
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
or just print(list(z)). As to second question - you print the result of p1+p2 - i.e. print(p1 + p2). The add method returns Polynominal object, so when you print it and because you don't have str method, it calls the repr method of Polynominal class
– buran
Nov 15 '18 at 9:22
add a comment |
When in a function declaration, * is list of args send.
For exemple:
def my_function(a, *b, **c):
print(a)
print(b)
print(c)
my_function("1st arg", "other arg", "other arg again", 2, arg_type="kwargs")
output :
1st args
["other arg", "other arg again", 2]
"arg_type": "kwargs"
And when is not in function declaration, it's for unpack the list.
For exemple:
list_of_arguments = ['a', 'b', 'z']
my_str = "the first letter of alphabet is , the second is and the last is "
print(my_str.format(*list_of_arguments))
or other exemple
def my_second_func(a, b, c):
print(a)
print(b)
my_list = [1, 2, 3]
my_second_func(a, b, c)
will output:
1
2
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
i edited my post, in your code*
is used for unpack the list returned by the zip() function
– iElden
Nov 15 '18 at 9:22
add a comment |
When in a function declaration, * is list of args send.
For exemple:
def my_function(a, *b, **c):
print(a)
print(b)
print(c)
my_function("1st arg", "other arg", "other arg again", 2, arg_type="kwargs")
output :
1st args
["other arg", "other arg again", 2]
"arg_type": "kwargs"
And when is not in function declaration, it's for unpack the list.
For exemple:
list_of_arguments = ['a', 'b', 'z']
my_str = "the first letter of alphabet is , the second is and the last is "
print(my_str.format(*list_of_arguments))
or other exemple
def my_second_func(a, b, c):
print(a)
print(b)
my_list = [1, 2, 3]
my_second_func(a, b, c)
will output:
1
2
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
i edited my post, in your code*
is used for unpack the list returned by the zip() function
– iElden
Nov 15 '18 at 9:22
add a comment |
When in a function declaration, * is list of args send.
For exemple:
def my_function(a, *b, **c):
print(a)
print(b)
print(c)
my_function("1st arg", "other arg", "other arg again", 2, arg_type="kwargs")
output :
1st args
["other arg", "other arg again", 2]
"arg_type": "kwargs"
And when is not in function declaration, it's for unpack the list.
For exemple:
list_of_arguments = ['a', 'b', 'z']
my_str = "the first letter of alphabet is , the second is and the last is "
print(my_str.format(*list_of_arguments))
or other exemple
def my_second_func(a, b, c):
print(a)
print(b)
my_list = [1, 2, 3]
my_second_func(a, b, c)
will output:
1
2
When in a function declaration, * is list of args send.
For exemple:
def my_function(a, *b, **c):
print(a)
print(b)
print(c)
my_function("1st arg", "other arg", "other arg again", 2, arg_type="kwargs")
output :
1st args
["other arg", "other arg again", 2]
"arg_type": "kwargs"
And when is not in function declaration, it's for unpack the list.
For exemple:
list_of_arguments = ['a', 'b', 'z']
my_str = "the first letter of alphabet is , the second is and the last is "
print(my_str.format(*list_of_arguments))
or other exemple
def my_second_func(a, b, c):
print(a)
print(b)
my_list = [1, 2, 3]
my_second_func(a, b, c)
will output:
1
2
edited Nov 15 '18 at 9:18
answered Nov 15 '18 at 9:16
iEldeniElden
721618
721618
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
i edited my post, in your code*
is used for unpack the list returned by the zip() function
– iElden
Nov 15 '18 at 9:22
add a comment |
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
i edited my post, in your code*
is used for unpack the list returned by the zip() function
– iElden
Nov 15 '18 at 9:22
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
but add does not have a parameter with a *
– J.doe
Nov 15 '18 at 9:18
i edited my post, in your code
*
is used for unpack the list returned by the zip() function– iElden
Nov 15 '18 at 9:22
i edited my post, in your code
*
is used for unpack the list returned by the zip() function– iElden
Nov 15 '18 at 9:22
add a comment |
Consider this easy example:
a = [1, 2, 3]
b = [4, 5, 6]
gen = (x + y for x, y in zip(a, b))
print(gen) ## this will print <generator object <genexpr> at 0x...>
In this case, the asterisk evaluates the generator expression. So when doing
print(*gen) ## this will print 5 7 9
you evaluate the generator expression.
add a comment |
Consider this easy example:
a = [1, 2, 3]
b = [4, 5, 6]
gen = (x + y for x, y in zip(a, b))
print(gen) ## this will print <generator object <genexpr> at 0x...>
In this case, the asterisk evaluates the generator expression. So when doing
print(*gen) ## this will print 5 7 9
you evaluate the generator expression.
add a comment |
Consider this easy example:
a = [1, 2, 3]
b = [4, 5, 6]
gen = (x + y for x, y in zip(a, b))
print(gen) ## this will print <generator object <genexpr> at 0x...>
In this case, the asterisk evaluates the generator expression. So when doing
print(*gen) ## this will print 5 7 9
you evaluate the generator expression.
Consider this easy example:
a = [1, 2, 3]
b = [4, 5, 6]
gen = (x + y for x, y in zip(a, b))
print(gen) ## this will print <generator object <genexpr> at 0x...>
In this case, the asterisk evaluates the generator expression. So when doing
print(*gen) ## this will print 5 7 9
you evaluate the generator expression.
answered Nov 15 '18 at 9:27
DocDrivenDocDriven
1,2652721
1,2652721
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%2f53315930%2fmeaning-of-operator-when-used-with-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
2
Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
– Norrius
Nov 15 '18 at 9:19