How to do this equivalent C# OOP in python?
I need following structure in python.
public class Circle
{
int member1;
int member2;
int member3;
public Circle(member1)
{
this.member1 = member1;
initializeRest();
}
private intializeRest()
{
//do lot of computation to get result1 & result2
this.member2 = result2;
this.member3 = result2;
}
开发者_JAVA技巧}
class Circle:
def __init__(self, member1):
self.member1=member1
self.rest()
def rest(self):
self.member2=result2
self.member3=result2
Python does not have enforced private anything; the convention for letting others know that a method/function/class/what-have-you is private is to prefix it with a single leading underscore. Third-party programs can use this, such as auto-documenting systems, and help() in IDLE, to ignore such _names.
Your code would translate like this:
class Circle(object): # in Python 3 you don't need `object`
member1 = None # not needed since all three are initialized
member2 = None # in __init__, but if you had others that
member3 = None # weren't this is how you would do it
def __init__(self, member1):
self.member1=member1
self._rest()
def _rest(self):
# lots of computing....
self.member2=result2
self.member3=result2
Given your comments in the code, though, you would be just as well off to make _rest
be part of __init__
...
class Circle(object): # in Python 3 you don't need `object`
def __init__(self, member1):
self.member1=member1
# lots of computing....
self.member2=result2
self.member3=result2
精彩评论