1

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?

5
  • 3
    You're using a lot of words there I'm not sure you know the meanings of. What? Commented Oct 28, 2009 at 0:29
  • @NSD may be as i told i am very new to python and may not aware of the python jargon. Commented Oct 28, 2009 at 0:33
  • i think its more a matter of grammatical construction than big words Commented Oct 28, 2009 at 0:59
  • @All Sorry, may be its because english is not my first language. Commented Oct 28, 2009 at 1:02
  • checkout the power of introspection diveintopython.org/power_of_introspection/index.html Commented Oct 28, 2009 at 1:14

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But dont you think this "too much" freedom can cause lot of problem in big projects?
@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.
you should mention hasattr and sisters
@itsaboutcode, Big projects usually have a style guide that would cover preferences such as this
2

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

2 Comments

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?
If you dont write c=None in __init__ you can only create the object with 3 parameters.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.