2

When I create a class in Python, I have to declare variables with self.:

class ClassName(object):
    def __init__(self, arg):
        super(ClassName, self).__init__()
        self.arg = arg

Great, but to instantiate it, I need to pass arguments to the init the method...but what if I don't have them, or don't want to bother with them?

I want something like

def __init__(self):
        super(ClassName, self).__init__()
        self.arg

...to later on refer to an existing but empty arg. Edit. By existing but empty I meant C-like behaviour:

int a;

...where variable a exists but has no value. Even gcc refers to that as empty , so I presumed that question would be clear.

Default values are as close as probably could be to what I want, I will use that.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    Why do you need this ? Just say None or declare it whenever you need inside the class – myildirim Jul 01 '14 at 11:45
  • 1
    Why not just *not assign anything*? You can always give an attribute a default value as a class attribute instead (but be careful with mutable class attributes). – Martijn Pieters Jul 01 '14 at 11:45
  • I am asking in plain English whether this is considered Pythonic and get down-vote because it's not. Great. –  Jul 01 '14 at 11:48
  • Could you be more specific about what you're trying to achieve, what you have tried so far and how it's not working out for you? – jonrsharpe Jul 01 '14 at 11:49
  • 1
    How about `self.arg = None`? Or better, just provide a default parameter: `__init__(self, arg=None)` – tobias_k Jul 01 '14 at 11:50
  • 1
    @ror6ax https://docs.python.org/2/tutorial/controlflow.html#default-argument-values – jonrsharpe Jul 01 '14 at 11:51
  • Your question is useless without defining what you mean with "empty" in "existing but empty arg". `None`? Simply assign `None`. Not set? Use `hasattr`. Default value? Use default arguments. Something else entirely? It's probably not useful, think of a better way. – l4mpi Jul 01 '14 at 14:31
  • 2
    @ror6ax: You are starting from a misconception: Python has no declarations, only assignments. When you make a class, you do not have to "declare" the variables in the `__init__`. You can simply assign the attribute when you have something to assign to it. – Ned Batchelder Jul 01 '14 at 20:34

2 Answers2

13

Option 1: Don't declare them at all

It seems that you come from a static language (Java, C#, C++) and you have difficulty adapting to Python. Since Python is dynamic, you can add and delete attributes from it any time you want, both inside and outside the class' methods. If you don't have an appropriate value, you don't declare it in __init__, declare it in some other method, that's perfectly fine.

Option 2. Set some default value

What exactly do you mean by an "existing but empty" value? What does it mean to be empty? In (some) statically typed languages, member values that you don't initialize explicitly are initialized automatically to their default value (integers initialized to 0, pointers/ references to null, etc). If you know an appropriate default value, just initialize your arg to that value; if there is no appropriate default, go to option 1.

2a. Assign 'manually'

def __init__(self):
    self.arg1 = 0
    self.arg2 = 'a'
    self.arg3 = None

2b. Through default function arguments

def __init__(self, arg1=0, arg2='a', arg3=None):
    self.arg1 = arg1
    self.arg2 = arg2
    self.arg3 = arg3

2c. Use properties (read more)

class ClassName(object):
    @property
    def arg1(self):
        return self._arg1 if hasattr(self, '_arg1') else 0

    @arg1.setter
    def arg1(self, value):
        self._arg1 = value
Community
  • 1
  • 1
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
0

The usual way to do this is to provide a default value for that parameter in the constructor:

class ClassName(object):
    def __init__(self, arg=None):
        super(ClassName, self).__init__()
        self.arg = arg

Exmaple:

>>> print ClassName().arg
None
>>> print ClassName("foo").arg
foo

Of course, you could use any other useful value as default instead of None, but be wary with using mutable data types, like lists.

Community
  • 1
  • 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179