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?
What is Gilligan's full name?
How can I find out about the game world without meta-influencing it?
Why is my Taiyaki (Cake that looks like a fish) too hard and dry?
Part of my house is inexplicably gone
In American Politics, why is the Justice Department under the President?
Fastest way from 8 to 7
Is it possible to have battery technology that can't be duplicated?
Is plausible to have subspecies with & without separate sexes?
How can powerful telekinesis avoid violating Newton's 3rd Law?
Why would a home insurer offer a discount based on credit score?
Do Veracrypt encrypted volumes have any kind of brute force protection?
Jam with honey & without pectin has a saucy consistency always
How to represent jealousy in a cute way?
If absolute velocity does not exist, how can we say a rocket accelerates in empty space?
How (un)safe is it to ride barefoot?
How do I properly use a function under a class?
Can I use 220 V outlets on a 15 ampere breaker and wire it up as 110 V?
What does BREAD stand for while drafting?
How to import .txt file with missing data?
Can you open the door or die? v2
Idiom for 'person who gets violent when drunk"
As easy as Three, Two, One... How fast can you go from Five to Four?
In Pandemic, why take the extra step of eradicating a disease after you've cured it?
Is all-caps blackletter no longer taboo?
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
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
|
show 6 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
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
13 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
13 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with()in C#.
– Goyo
13 hours ago
1
@KunalMukherjee yes it is - theMyChar.Score()expression first resolves the"Score"attribute onMyCharobject (yielding a method object), then applies the call operator (the parens) on it.
– bruno desthuilliers
13 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
13 hours ago
|
show 6 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
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 26 mins ago
Peter Mortensen
14.1k1988114
14.1k1988114
New contributor
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 13 hours ago
FrankWFrankW
401
401
New contributor
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
FrankW is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
13 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
13 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with()in C#.
– Goyo
13 hours ago
1
@KunalMukherjee yes it is - theMyChar.Score()expression first resolves the"Score"attribute onMyCharobject (yielding a method object), then applies the call operator (the parens) on it.
– bruno desthuilliers
13 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
13 hours ago
|
show 6 more comments
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
13 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
13 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with()in C#.
– Goyo
13 hours ago
1
@KunalMukherjee yes it is - theMyChar.Score()expression first resolves the"Score"attribute onMyCharobject (yielding a method object), then applies the call operator (the parens) on it.
– bruno desthuilliers
13 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
13 hours ago
3
3
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
13 hours ago
Score is not an attribute but a member function, invoke it like print(MyChar.Score())
– Kunal Mukherjee
13 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
13 hours ago
@Kunal It could be, though. This is especially useful when passing functions/methods into higher-order functions such as map/filter. :)
– TrebledJ
13 hours ago
2
2
I would swear that this kind of functions are also called methods and are invoked with
() in C#.– Goyo
13 hours ago
I would swear that this kind of functions are also called methods and are invoked with
() in C#.– Goyo
13 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
13 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
13 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
13 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
13 hours ago
|
show 6 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())
5
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
13 hours 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
12 hours 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.
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()
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())
5
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
13 hours ago
add a comment |
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
5
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
13 hours ago
add a comment |
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
Use it like any other function by calling it. Note the extra pair of parentheses below.
print(MyChar.Score())
answered 13 hours ago
TrebledJTrebledJ
5,11941435
5,11941435
5
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
13 hours ago
add a comment |
5
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
13 hours ago
5
5
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
13 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
13 hours 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
12 hours 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
12 hours 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 12 hours ago
Radeonx
327
327
answered 13 hours ago
Adam.Er8Adam.Er8
581212
581212
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
12 hours ago
add a comment |
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
12 hours ago
2
2
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
12 hours ago
While that's a nice suggestion, it doesn't really answer the OP's question.
– bruno desthuilliers
12 hours 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.
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.
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 12 hours ago
bruno desthuilliersbruno desthuilliers
53.1k54766
53.1k54766
add a comment |
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()
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()
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 12 hours ago
answered 12 hours ago
Saurav RaiSaurav Rai
815
815
add a comment |
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
13 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
13 hours ago
2
I would swear that this kind of functions are also called methods and are invoked with
()in C#.– Goyo
13 hours ago
1
@KunalMukherjee yes it is - the
MyChar.Score()expression first resolves the"Score"attribute onMyCharobject (yielding a method object), then applies the call operator (the parens) on it.– bruno desthuilliers
13 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
13 hours ago