I have an class defined in C# as Servicing and i need to convert this code to Python. So how do i convert the Servicing class to a list datatype in python and then use it in Adjusted class?
class Servicing
{
public long StatementName{ get; set; }
public string City{ get; set; }
}
Now this class is used in another class Adjusted
class Adjusted
{
public List<Servicing> Services{ get; set; }
}
For Servicing class I can define the constructor like this and then have its setter and getter defined too.
class Servicing:
def __init__(self):
self._StatementName=0.0
self._City= ""
But how do I use this Servicing class in a similar way how it is used in Adjusted class?
CodePudding user response:
class Adjusted:
def __init__(self):
self.Services = None
class Servicing:
def __init__(self):
self.StatementName = 0
self.City = None
Don't know much about Python, But it should be something like the above.
Please ignore compilation errors, As I am not from Pythn background.
CodePudding user response:
Python doesn't need setters or getters since all class attributes and methods are public. Instead values are given as parameters to __init__
and class is initialized that way. List are almost as easy, just add new values with .append
or extend with another iterable with .extend
class Servicing:
# Default values for class
def __init__(self, StatementName=0.0, City=""):
self.StatementName = StatementName
self.City = City
class Adjusted:
def __init__(self, Services=[]):
self.Services = []
self.Services.extend(Services)
def add_service(self, StatementName=0.0, City=""):
new_servicing = Servicing(StatementName, City)
self.Services.append(new_servicing)
a_servicing = Servicing(1.2, "London")
print(Servicing.City)
a_servicing.City = "Boston"
print(Servicing.City)
When you get more familiar with python, there is a way to implement getter/setter with the @property
decorator.