library

Learning property getter setter in Python

21 Feb , 2012  

Property is new feature started in Python 2.2. It have great function to return attribute property when class derived from object. For example :

1
2
3
4
5
6
7
8
9
10
11
class Insurance(object):
    def __init__(self):
        self.name = None
        print(self.name)

    def set_name(self):
        self.name = 'Popel'
        return self.name

a = Insurance()
print(a.set_name())
1
2
print(a.set_name())
# will print "Popel"

To get self.name, we should call method inside derived class, a.set_name().
Then how to change it into attribute ? This why we should know property.

Let see :

1
2
3
4
5
6
7
8
9
10
11
12
class Insurance(object):
    def __init__(self):
        self.name = None
        print(self.name)

    @property
    def set_name(self):
        self.name = 'Popel'
        return self.name

a = Insurance()
print(a.set_name)

We see that when Insurance() initialized, self.name contain “None”. By using property, we can create attribute called “set_name” and make self.name value into “Popel”.

1
2
print(a.set_name)
# will print "Popel"

Property is great feature in Python that developer should know :)

library

Deep understanding XML-RPC Server – Client in Python

6 Dec , 2011  

XMLRPC (XML Remote Procedure Call) which send XML through HTTP for transporting data is common way to communicate between Server and Client. Basically, Client can call methods with parameters on a remote server and get structured data for results. Python have XML-RPC standard library, which it’s mean native installed and ready to use.

First, I start with xmlrpclib (a class) for client access. There important objects inside xmlrpclib for communicate with remote server called ServerProxy(). Basically, you can start communicate with remote server by xmlrpclib.ServerProxy(URI).
More…

,