How do I properly use a function under a class?How to merge two dictionaries in a single expression?How do I...
How to soundproof the Wood Shop?
Can I get a photo of an Ancient Arrow?
How (un)safe is it to ride barefoot?
Did I need a visa in 2004 and 2006?
Is it true that "only photographers care about noise"?
Must I use my personal social media account for work?
Am I being scammed by a sugar daddy?
What's the relation between у.е. to USD?
Undocumented incompatibility between changes and siunitx?
LWC: detect last element in for:each iteration
Am I allowed to determine tenets of my contract as a warlock?
How to represent jealousy in a cute way?
Approach sick days in feedback meeting
Why would a home insurer offer a discount based on credit score?
Is Jesus the last Prophet?
Is time complexity more important than space complexity?
What did the 8086 (and 8088) do upon encountering an illegal instruction?
How do I properly use a function under a class?
How do I type a hyphen in iOS 12?
How can religions without a hell discourage evil-doing?
Why is it bad to use your whole foot in rock climbing
Are skill challenges an official option or homebrewed?
Can an open source licence be revoked if it violates employer's IP?
Keeping track of theme when improvising
How do I properly use a function under a class?
How to merge two dictionaries in a single expression?How do I check if a list is empty?Are static class variables possible?How do I check whether a file exists without exceptions?How to flush output of print function?How can I safely create a nested directory?Using global variables in a functionHow do I sort a dictionary by value?How to make a chain of function decorators?How do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am currently learning Python. Since I am a big fan of OO (object-oriented) programming, obviously it's not hard to apply it in Python. But when I tried it, it seems very different to C#.
As you can see below, I am trying to create a character class, with three attributes Id, Hp, and Mana. The score is calculated by adding up Hp and Mana and then times 10.
As you can see, after defining MyChar where id=10 hp=100 mana=100
, I was expecting MyChar.Score
is (100+100)*10, which is 2000, but weirdly, it says:
bound method Character.Score of <__main__.Character object at 0x0000021B17DD1F60>
as the result of print(MyChar.Score)
.
How can I fix this problem?
Here is my code:
class Character:
def __init__(self, Id, Hp, Mana):
self.Id = Id;
self.Hp = Hp;
self.Mana = Mana;
def Score(self):
return (self.Hp + self.Mana)*10;
MyChar = Character(10, 100, 100);
print(MyChar.Score)
python python-3.x
New contributor
|
show 7 more comments
I am currently learning Python. Since I am a big fan of OO (object-oriented) programming, obviously it's not hard to apply it in Python. But when I tried it, it seems very different to C#.
As you can see below, I am trying to create a character class, with three attributes Id, Hp, and Mana. The score is calculated by adding up Hp and Mana and then times 10.
As you can see, after defining MyChar where id=10 hp=100 mana=100
, I was expecting MyChar.Score
is (100+100)*10, which is 2000, but weirdly, it says:
bound method Character.Score of <__main__.Character object at 0x0000021B17DD1F60>
as the result of print(MyChar.Score)
.
How can I fix this problem?
Here is my code:
class Character:
def __init__(self, Id, Hp, Mana):
self.Id = Id;
self.Hp = Hp;
self.Mana = Mana;
def Score(self):
return (self.Hp + self.Mana)*10;
MyChar = Character(10, 100, 100);
print(MyChar.Score)
python python-3.x
New contributor
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
14 hours ago
1
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
14 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with()
in C#.
– Goyo
14 hours ago
1
@KunalMukherjee yes it is - theMyChar.Score()
expression first resolves the"Score"
attribute onMyChar
object (yielding a method object), then applies the call operator (the parens) on it.
– bruno desthuilliers
14 hours ago
1
@Goyo you may want to read this about what Python "methods" really are: wiki.python.org/moin/FromFunctionToMethod - as a general rule, Python's object model is wildly different from C#'s one, so while you'll find the same basic concepts of class, instance, attribute, method etc, you won't have a 1:1 mapping with the way C# implement those concepts.
– bruno desthuilliers
14 hours ago
|
show 7 more comments
I am currently learning Python. Since I am a big fan of OO (object-oriented) programming, obviously it's not hard to apply it in Python. But when I tried it, it seems very different to C#.
As you can see below, I am trying to create a character class, with three attributes Id, Hp, and Mana. The score is calculated by adding up Hp and Mana and then times 10.
As you can see, after defining MyChar where id=10 hp=100 mana=100
, I was expecting MyChar.Score
is (100+100)*10, which is 2000, but weirdly, it says:
bound method Character.Score of <__main__.Character object at 0x0000021B17DD1F60>
as the result of print(MyChar.Score)
.
How can I fix this problem?
Here is my code:
class Character:
def __init__(self, Id, Hp, Mana):
self.Id = Id;
self.Hp = Hp;
self.Mana = Mana;
def Score(self):
return (self.Hp + self.Mana)*10;
MyChar = Character(10, 100, 100);
print(MyChar.Score)
python python-3.x
New contributor
I am currently learning Python. Since I am a big fan of OO (object-oriented) programming, obviously it's not hard to apply it in Python. But when I tried it, it seems very different to C#.
As you can see below, I am trying to create a character class, with three attributes Id, Hp, and Mana. The score is calculated by adding up Hp and Mana and then times 10.
As you can see, after defining MyChar where id=10 hp=100 mana=100
, I was expecting MyChar.Score
is (100+100)*10, which is 2000, but weirdly, it says:
bound method Character.Score of <__main__.Character object at 0x0000021B17DD1F60>
as the result of print(MyChar.Score)
.
How can I fix this problem?
Here is my code:
class Character:
def __init__(self, Id, Hp, Mana):
self.Id = Id;
self.Hp = Hp;
self.Mana = Mana;
def Score(self):
return (self.Hp + self.Mana)*10;
MyChar = Character(10, 100, 100);
print(MyChar.Score)
python python-3.x
python python-3.x
New contributor
New contributor
edited 1 hour ago
Peter Mortensen
14.1k1988114
14.1k1988114
New contributor
asked 14 hours ago
FrankWFrankW
451
451
New contributor
New contributor
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
14 hours ago
1
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
14 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with()
in C#.
– Goyo
14 hours ago
1
@KunalMukherjee yes it is - theMyChar.Score()
expression first resolves the"Score"
attribute onMyChar
object (yielding a method object), then applies the call operator (the parens) on it.
– bruno desthuilliers
14 hours ago
1
@Goyo you may want to read this about what Python "methods" really are: wiki.python.org/moin/FromFunctionToMethod - as a general rule, Python's object model is wildly different from C#'s one, so while you'll find the same basic concepts of class, instance, attribute, method etc, you won't have a 1:1 mapping with the way C# implement those concepts.
– bruno desthuilliers
14 hours ago
|
show 7 more comments
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
14 hours ago
1
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
14 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with()
in C#.
– Goyo
14 hours ago
1
@KunalMukherjee yes it is - theMyChar.Score()
expression first resolves the"Score"
attribute onMyChar
object (yielding a method object), then applies the call operator (the parens) on it.
– bruno desthuilliers
14 hours ago
1
@Goyo you may want to read this about what Python "methods" really are: wiki.python.org/moin/FromFunctionToMethod - as a general rule, Python's object model is wildly different from C#'s one, so while you'll find the same basic concepts of class, instance, attribute, method etc, you won't have a 1:1 mapping with the way C# implement those concepts.
– bruno desthuilliers
14 hours ago
3
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
14 hours ago
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
14 hours ago
1
1
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
14 hours ago
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
14 hours ago
2
2
I would swear that this kind of functions are also called methods and are invoked with
()
in C#.– Goyo
14 hours ago
I would swear that this kind of functions are also called methods and are invoked with
()
in C#.– Goyo
14 hours ago
1
1
@KunalMukherjee yes it is - the
MyChar.Score()
expression first resolves the "Score"
attribute on MyChar
object (yielding a method object), then applies the call operator (the parens) on it.– bruno desthuilliers
14 hours ago
@KunalMukherjee yes it is - the
MyChar.Score()
expression first resolves the "Score"
attribute on MyChar
object (yielding a method object), then applies the call operator (the parens) on it.– bruno desthuilliers
14 hours ago
1
1
@Goyo you may want to read this about what Python "methods" really are: wiki.python.org/moin/FromFunctionToMethod - as a general rule, Python's object model is wildly different from C#'s one, so while you'll find the same basic concepts of class, instance, attribute, method etc, you won't have a 1:1 mapping with the way C# implement those concepts.
– bruno desthuilliers
14 hours ago
@Goyo you may want to read this about what Python "methods" really are: wiki.python.org/moin/FromFunctionToMethod - as a general rule, Python's object model is wildly different from C#'s one, so while you'll find the same basic concepts of class, instance, attribute, method etc, you won't have a 1:1 mapping with the way C# implement those concepts.
– bruno desthuilliers
14 hours ago
|
show 7 more comments
4 Answers
4
active
oldest
votes
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
The pair of parentheses following MyChar.Score
are needed to invoke the __call__()
magic method, calling the function. Without these parentheses, only the representation of the method, repr(MyChar.Score)
, was printed.
6
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Thank you, get it now!
– FrankW
11 mins ago
add a comment |
If you want to use it like a property in C#, decorate the function with @property
, like so:
class Character:
def __init__(self,Id,Hp,Mana):
self.Id=Id;
self.Hp=Hp;
self.Mana=Mana;
@property
def Score(self):
return (self.Hp+self.Mana)*10;
MyChar=Character(10,100,100);
print(MyChar.Score)
So you don't have to call it like a function.
For more advanced usage of properties (e.g. also having a setter func), see the official docs: https://docs.python.org/3/library/functions.html#property
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
In Python, everything is an object, including classes, functions and methods, so MyChar.Score
(without the parens) only resolves the Score
attribute on MyChar
object. This yields a method
object, which happens to be a callable object (an object that implements the __call__
special method). You then have to apply the call operator (the parens) to actually call it.
You may want to check the official documentation for more on Python's object model.
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
class Character(object):
def __init__(self):
print ('Starting')
def method(self):
print ('This is a method()')
ch = Character()
'''When we dont add the bracket after the method call it would lead to method bound error as in your case'''
print (ch.method)
'''This can be solved by doing the following line'''
ch.method()
Thank you, get it now!
– FrankW
10 mins ago
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
});
}
});
FrankW is a new contributor. Be nice, and check out our Code of Conduct.
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%2f56542562%2fhow-do-i-properly-use-a-function-under-a-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
The pair of parentheses following MyChar.Score
are needed to invoke the __call__()
magic method, calling the function. Without these parentheses, only the representation of the method, repr(MyChar.Score)
, was printed.
6
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Thank you, get it now!
– FrankW
11 mins ago
add a comment |
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
The pair of parentheses following MyChar.Score
are needed to invoke the __call__()
magic method, calling the function. Without these parentheses, only the representation of the method, repr(MyChar.Score)
, was printed.
6
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Thank you, get it now!
– FrankW
11 mins ago
add a comment |
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
The pair of parentheses following MyChar.Score
are needed to invoke the __call__()
magic method, calling the function. Without these parentheses, only the representation of the method, repr(MyChar.Score)
, was printed.
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
The pair of parentheses following MyChar.Score
are needed to invoke the __call__()
magic method, calling the function. Without these parentheses, only the representation of the method, repr(MyChar.Score)
, was printed.
edited 37 mins ago
answered 14 hours ago
TrebledJTrebledJ
5,12941435
5,12941435
6
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Thank you, get it now!
– FrankW
11 mins ago
add a comment |
6
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Thank you, get it now!
– FrankW
11 mins ago
6
6
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Without the parentheses, you were not calling the function. Instead, you were printing a representation of the function itself, which is a method of the character class called 'Score'.
– Deepstop
14 hours ago
Thank you, get it now!
– FrankW
11 mins ago
Thank you, get it now!
– FrankW
11 mins ago
add a comment |
If you want to use it like a property in C#, decorate the function with @property
, like so:
class Character:
def __init__(self,Id,Hp,Mana):
self.Id=Id;
self.Hp=Hp;
self.Mana=Mana;
@property
def Score(self):
return (self.Hp+self.Mana)*10;
MyChar=Character(10,100,100);
print(MyChar.Score)
So you don't have to call it like a function.
For more advanced usage of properties (e.g. also having a setter func), see the official docs: https://docs.python.org/3/library/functions.html#property
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
If you want to use it like a property in C#, decorate the function with @property
, like so:
class Character:
def __init__(self,Id,Hp,Mana):
self.Id=Id;
self.Hp=Hp;
self.Mana=Mana;
@property
def Score(self):
return (self.Hp+self.Mana)*10;
MyChar=Character(10,100,100);
print(MyChar.Score)
So you don't have to call it like a function.
For more advanced usage of properties (e.g. also having a setter func), see the official docs: https://docs.python.org/3/library/functions.html#property
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
If you want to use it like a property in C#, decorate the function with @property
, like so:
class Character:
def __init__(self,Id,Hp,Mana):
self.Id=Id;
self.Hp=Hp;
self.Mana=Mana;
@property
def Score(self):
return (self.Hp+self.Mana)*10;
MyChar=Character(10,100,100);
print(MyChar.Score)
So you don't have to call it like a function.
For more advanced usage of properties (e.g. also having a setter func), see the official docs: https://docs.python.org/3/library/functions.html#property
If you want to use it like a property in C#, decorate the function with @property
, like so:
class Character:
def __init__(self,Id,Hp,Mana):
self.Id=Id;
self.Hp=Hp;
self.Mana=Mana;
@property
def Score(self):
return (self.Hp+self.Mana)*10;
MyChar=Character(10,100,100);
print(MyChar.Score)
So you don't have to call it like a function.
For more advanced usage of properties (e.g. also having a setter func), see the official docs: https://docs.python.org/3/library/functions.html#property
edited 13 hours ago
Radeonx
327
327
answered 14 hours ago
Adam.Er8Adam.Er8
591212
591212
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
Thank you, get it now!
– FrankW
10 mins ago
2
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
14 hours ago
Thank you, get it now!
– FrankW
10 mins ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
In Python, everything is an object, including classes, functions and methods, so MyChar.Score
(without the parens) only resolves the Score
attribute on MyChar
object. This yields a method
object, which happens to be a callable object (an object that implements the __call__
special method). You then have to apply the call operator (the parens) to actually call it.
You may want to check the official documentation for more on Python's object model.
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
In Python, everything is an object, including classes, functions and methods, so MyChar.Score
(without the parens) only resolves the Score
attribute on MyChar
object. This yields a method
object, which happens to be a callable object (an object that implements the __call__
special method). You then have to apply the call operator (the parens) to actually call it.
You may want to check the official documentation for more on Python's object model.
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
In Python, everything is an object, including classes, functions and methods, so MyChar.Score
(without the parens) only resolves the Score
attribute on MyChar
object. This yields a method
object, which happens to be a callable object (an object that implements the __call__
special method). You then have to apply the call operator (the parens) to actually call it.
You may want to check the official documentation for more on Python's object model.
In Python, everything is an object, including classes, functions and methods, so MyChar.Score
(without the parens) only resolves the Score
attribute on MyChar
object. This yields a method
object, which happens to be a callable object (an object that implements the __call__
special method). You then have to apply the call operator (the parens) to actually call it.
You may want to check the official documentation for more on Python's object model.
answered 14 hours ago
bruno desthuilliersbruno desthuilliers
53.1k54766
53.1k54766
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
Thank you, get it now!
– FrankW
10 mins ago
Thank you, get it now!
– FrankW
10 mins ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
class Character(object):
def __init__(self):
print ('Starting')
def method(self):
print ('This is a method()')
ch = Character()
'''When we dont add the bracket after the method call it would lead to method bound error as in your case'''
print (ch.method)
'''This can be solved by doing the following line'''
ch.method()
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
class Character(object):
def __init__(self):
print ('Starting')
def method(self):
print ('This is a method()')
ch = Character()
'''When we dont add the bracket after the method call it would lead to method bound error as in your case'''
print (ch.method)
'''This can be solved by doing the following line'''
ch.method()
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
class Character(object):
def __init__(self):
print ('Starting')
def method(self):
print ('This is a method()')
ch = Character()
'''When we dont add the bracket after the method call it would lead to method bound error as in your case'''
print (ch.method)
'''This can be solved by doing the following line'''
ch.method()
class Character(object):
def __init__(self):
print ('Starting')
def method(self):
print ('This is a method()')
ch = Character()
'''When we dont add the bracket after the method call it would lead to method bound error as in your case'''
print (ch.method)
'''This can be solved by doing the following line'''
ch.method()
edited 14 hours ago
answered 14 hours ago
Saurav RaiSaurav Rai
815
815
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
Thank you, get it now!
– FrankW
10 mins ago
Thank you, get it now!
– FrankW
10 mins ago
Thank you, get it now!
– FrankW
10 mins ago
add a comment |
FrankW is a new contributor. Be nice, and check out our Code of Conduct.
FrankW is a new contributor. Be nice, and check out our Code of Conduct.
FrankW is a new contributor. Be nice, and check out our Code of Conduct.
FrankW is a new contributor. Be nice, and check out our Code of Conduct.
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%2f56542562%2fhow-do-i-properly-use-a-function-under-a-class%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
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
14 hours ago
1
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
14 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with
()
in C#.– Goyo
14 hours ago
1
@KunalMukherjee yes it is - the
MyChar.Score()
expression first resolves the"Score"
attribute onMyChar
object (yielding a method object), then applies the call operator (the parens) on it.– bruno desthuilliers
14 hours ago
1
@Goyo you may want to read this about what Python "methods" really are: wiki.python.org/moin/FromFunctionToMethod - as a general rule, Python's object model is wildly different from C#'s one, so while you'll find the same basic concepts of class, instance, attribute, method etc, you won't have a 1:1 mapping with the way C# implement those concepts.
– bruno desthuilliers
14 hours ago