tags:

views:

152

answers:

3

Hi,

I am newbie and finding it very hard to grasp the syntax of Class in python. I have a background of C/C++, java and objective C. A very big difference which i am noticing in python is that you don't explicitly declare the "data members" in the class and you just randomly add them? And it leads to quite big confusion.

Let say i have a class

class MyClass:

    def __int__(self, a, b):

        self.a = a

        self.b = b

And then when i initiate the object.

myobject = MyClass(10,10)

And just after some time for some reason i come to know that i need another parameter in this class but i dont wanted to initiate that using constructor because it will be initiated by another function depending on the some particular condition, so in whole mess of code that will be only point that variable actually get birth. is not the case when i will be checking the code while debugging or reviewing it for some other reason it will be confusing?

+2  A: 

You should really use some . in your text :p

Could you mean:

class MyClass:
    def __int__(self, a, b, c=None):
        self.a = a
        self.b = b
        self.c = c

one = MyClass(1,2)
one.c # None
two = MyClass(1,2,3)
two.c # 3
THC4k
it looks to be good solution but what if someone forget for some reason to assign this new field to None, dont you think its too ease is cause of lot of problems?
itsaboutcode
If you dont write `c=None` in `__init__` you can only create the object with 3 parameters.
THC4k
I think that saying is right, python is for adults not for kids lox...
itsaboutcode
+5  A: 

In short, Yes.

You're right. Python lets you add (and remove!) members from objects at will, at any time. There's nothing special about a constructor that allows it to do anything that other functions can't.

If you want to be sure that all instances of your class have the same members at all times, then by all means assign them all in the constructor, using a sentinel value like None for ones that don't have a meaningful value yet, and avoid adding new members outside the constructor.

It's up to you how you manipulate your objects, and if you want to do that in a static fashion then that's fine, or if you want to take advantage of the ability to add and remove members at arbitrary times, that's fine too. Python itself doesn't impose (m)any rules.

RichieHindle
But dont you think this "too much" freedom can cause lot of problem in big projects?
itsaboutcode
@itsaboutcode: Yes, yes it can. That's why Python allows you to *not* add arbitrary members at arbitrary times, should you choose not to. You can make up your own rules and choose to stick to them, or not.
RichieHindle
you should mention `hasattr` and sisters
hasen j
@itsaboutcode, Big projects usually have a style guide that would cover preferences such as this
gnibbler
A: 
class MyClass:
    def __int__(self, a, b):
        self.a = a
        self.b = b
        self.c = None  #This line is optional

    def set_c(self, c):
        self.c = c

Some people prefer to list all the attributes in the __init__. You don't have to, but there are any number of reasons you might choose to.
Maybe it improves your editor's ability to understand your code for highlighting or completion.
Maybe it is just a style that you prefer.

gnibbler