Correct Way to Write this c# code in Python

This is the place for queries that don't fit in any of the other categories.

Correct Way to Write this c# code in Python

Postby sagarvarule » Sat Aug 03, 2013 6:14 am

Hi All,

Im new to Python. Im coming from C# background and want to learn Python.
I was used to do following thing in C# in my previous experiences. I want to know how do I implement below example in Python. How these things are done in Python.
Code: Select all
public class Bank
{
   
   public List<Customer> lstCustomers = new List<Customer>();
   private string micrcode;
   
   public void Bank()
   {
      customer
   }

}

public class Customer
{
   private srting customername;
   
   public string CustomerName
   
   {
      get { return customername; }
      set { customername = value; }
   }
}

main()
{
   Customer objCustomer = new Customer;
   objCustomer.CustomerName = "XYZ"
   
   Bank objBank = new Bank();
   objBank.lstCustomer.Add(objCustomer);
   
}
Last edited by Yoriz on Sat Aug 03, 2013 11:41 am, edited 1 time in total.
Reason: modified title
sagarvarule
 
Posts: 2
Joined: Sat Aug 03, 2013 5:46 am

Re: Correct Way to Write in Python

Postby stranac » Sat Aug 03, 2013 8:53 am

Read the official python tutorial.
It has a section on classes.

When you're done, try coding it. If you have problems, ask us specific questions.

Also, you shouldn't use classes for every little thing, at least not in python.
But I'm assuming you know that, and this is just an example.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Correct Way to Write this c# code in Python

Postby metulburr » Sat Aug 03, 2013 12:44 pm

i am going to answer this with code, because 1) it seems legit to want to know how to convert from a language you know to a new language, and 2) i am just starting recently to get into c++ and it intrigued me also and 3) i have been doing c++ and not c# so i dont know the line between the two. So in short, it could be wrong, and you still have to check the documentation that stranac gave.

Code: Select all
class Bank:
   lstCustomers = []
   __mircode = ''
   def __init__(self):
      pass#customer

class Customer:
   __customername = ''
   customername = ''
   
objCustomer = Customer()
objCustomer.CustomerName = "XYZ"

objBank = Bank()
objBank.lstCustomer.append(objCustomer)

Now the for th things i dont know:
i dont understand the syntax for the getter and setter in that c# snippet. So i just left it out. But you get the jist. I dont know how to if there is a way to store anything on the heap in python. I have hardly seen private vars in python. I dont beleive its used as heavely as its used in C++ and the like. The underscores are the syntax to describe a private var in python. __init__() is the constructor. There is no destructor.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Correct Way to Write this c# code in Python

Postby sagarvarule » Sat Aug 03, 2013 5:05 pm

Thanks for all the help. Has really cleared some concept of python.


Thanks a lot...!!!!
sagarvarule
 
Posts: 2
Joined: Sat Aug 03, 2013 5:46 am

Re: Correct Way to Write this c# code in Python

Postby ochichinyezaboombwa » Mon Aug 05, 2013 6:13 am

The best correct way to translate this to Python I think would be:
a) translate it to Java 1st;
b) quickly back to c#;
c) come here with the modified, beautified version, and the same question.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Correct Way to Write this c# code in Python

Postby micseydel » Mon Aug 05, 2013 7:28 am

I hope you're mocking Java.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1119
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests

cron