Can someone tell me what is the different between default_age and self.default_age?
Only one default_age variable is created for all instance of class Student and self.default_age means very instances of class Student gets a variable default_age?

How do you specify a static class(singleton class) in python?

class Student: 
   default_age = 20               # base class variable 
   def __init__(self, age): 
               self.default_age = age

Only one default_age variable is created for all instance of class Student and self.default_age means every instances of class Student gets a variable default_age?

No. In your example you've defined variable Student.default_age which has nothing to do with any default_age property that a class instance may have. The class instance default_age property gets created on the fly when it is applied to a class instance. This is true of all properties, and does not need to happen "inside" the class definition code.

I don't think there's any language primitive that allows you to create static classes in Python. You can achieve the same effect by redefining the __init__() constructor along the lines of:

class once:
    def __init__(self, data):
        self.data = data
        once.__init__ = once.__nomore__ # Redefine away the __init__ constructor
        return
    def __nomore__(self, ignored):
        print "No further entries can be created"
        return

my_once  = once("Never again")  # Works
my_twice = once("Let's re-try") # Prints No further entries can be created

If you want to be even more explicit, just set once.__init__ = None , which will cause a TypeError exception if anybody tries to make another once.

Can someone explain to me what is going on here? In this example "a" and "self.a" both same? t, t2 are instance of Truc and when I do:

>>>Truc.gotcha()
>>> print Truc.a
1

Why doesn't Truc.gotcha() incerment it by 1, so "print Truc.a" would be "2", not "1"?

>>> class Truc:
...     a=1
...     def gotcha(self):
...         self.a +=1
...         print self.a
... 
>>> print Truc.a
1
>>> t=Truc()
>>> print t.a
1
>>> t.gotcha()
2
>>> print Truc.a
1
>>> t2=Truc()
>>> print t2.a
1
>>> t2.gotcha()
2
>>> print Truc.a
1
>>> t.gotcha()
3
>>> print t.a
3
>>> print t2.a
2
>>> print Truc.a
1
>>>Truc.gotcha() 
>>> print Truc.a
1

You probably need to read up on Python namespaces. When you say t=Truc() you define a class instance t that has its own namespace. When you first request the value of t.a Python sees there is no attribute a of t, and looks in outer namespaces, finding the class variable Truc.a and returns that value. But if you later say t.a=101 you create a new attribute t.a that has no relationship to Truc.a. Subsequent requests for the value t.a find a in t's namespace and return that value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.