-2

I'm new to OOP and writing the class which requires many(>=9) defaults "hard coded" parameters used only with in this.

What is the right way of defining those inside the class?

Solution#1

class SomeCLass():
    __param1 = 'param1'
    __param2 = 'param2'
    __param3 = 'param3'
    __param4 = 'param4'

   def some_method(self):
       return self.__param1 

Or I should be doing it this way

Solution #2

class SomeCLass():

    def __init__(self, param1 = 'param1', param2 = 'param2', ...., 
                 ...param8 = 'param'):
         self.param1 = param1

Thank you

yadayada
  • 327
  • 1
  • 3
  • 14
  • Those two things are not the same, which you need depends on your specific context. – jonrsharpe Aug 16 '17 at 20:47
  • 2
    You are using *class attributes* in the first example. In the second example, your `__init__` function has a bunch of default parameters, which presumably, you will set to *instance attributes*. These are different situations. Also, don't use double-underscore name-mangling unless you understand what it is doing, and even then, probably not... – juanpa.arrivillaga Aug 16 '17 at 20:51
  • Also, while class-attributes are similar to static members in languages like java, they are not really equivalent to default parameters at all. – juanpa.arrivillaga Aug 16 '17 at 20:53
  • @juanpa.arrivillaga wasn't asking about java, if you have no advices, regarding actual question you don't have comment, thanks – yadayada Aug 16 '17 at 20:54
  • @jonrsharpe thanks will read more then – yadayada Aug 16 '17 at 20:55
  • Yes, well, you are using the java/c++ terminology. And regardless, my answer was about Python anyways... – juanpa.arrivillaga Aug 16 '17 at 20:55
  • All my comments were *directly* about Python best-practices. – juanpa.arrivillaga Aug 16 '17 at 20:59
  • You should check this [link](https://stackoverflow.com/questions/68645/static-class-variables-in-python#27568860), seems like what you trying to do. – Aswer Aug 16 '17 at 21:11
  • Yep, thank you Immutable "Static Variables", this is what I need – yadayada Aug 16 '17 at 21:14
  • You asked the question "what is the right way to do this?" Your real question is "what is the difference between these?" – Matthew Cole Aug 16 '17 at 21:15
  • I ask my question the way I think it should be asked to receive the answer I'm interested in – yadayada Oct 27 '17 at 20:59

1 Answers1

0

Short answer: solution 1 will have fields named paramN, solution 2 will have arguments to the constructor, but not the fields in the class instance.

The easy way to do this is to demonstrate it. I'm going to change the names of your classes so they don't conflict, and I'm going to limit each to two params for brevity.

class SomeClass1():
    param1 = 'param1'
    param2 = 'param2'

    def some_method(self):
        return self.param1

class SomeClass2():
    def __init__(self, param1 = 'param1', param2 = 'param2'):
        pass

    def some_method(self):
        return self.param1

# prints the string 'param1'
one = SomeClass1()
print(one.some_method())

# throws a NameError, 
# because SomeClass2 instances don't have the field
two = SomeClass2()
print(two.some_method())

If you want SomeClass2 to have the param fields, you'll need to handle that in the __init()__ method by capturing the arguments:

class SomeClass2():
    def __init__(self, param1 = 'param1', param2 = 'param2'):
        self.param1 = param1
        self.param2 = param2

Be warned though, those arguments are not hard coded as you requested.

# Prints the integer 1, not the string 'param1'
two = SomeClass2(param1 = 1, param2 = 2)
print(two.some_method())     

As to which is correct, well, that depends on what you're trying to do. Both solutions are valid python code.

Matthew Cole
  • 602
  • 5
  • 21