Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
PLATYPOS
PLATYPOS
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 10
    • Issues 10
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Laura Ketzer
  • PLATYPOSPLATYPOS
  • Issues
  • #8

Closed
Open
Created Jun 24, 2020 by Yori Fournier@yfournierReporter

CLEANING: dictionary in constructor are usually not a good idea

That is not what you are suppose to do.

What do you want to achieve with a dictionary? Why not using keywords? If it is to avoid writing all the arguments when calling the function be aware that there are method for that:

class MySuperClass(object):
    '''This is a dummy class just for you
    
    Attributes:
    -----------
    dummy_member: float
        That is the main part of this class.

    '''

    def __init__(self, dummy_member, time_ten=False, multiplicator=None, power_of_two=True, *args, **kwargs):
        '''Initialize a new instance (=object) of MySuperClass

        Parameters:
        -----------
        dummy_member : float
            value of the dummy_member

        time_ten : bool, optional, default=False
            if set to True dummy_member value will be multiplied by 10

        multiplicator : float, optional, default=None
            if given multiplies the dummy_member value.

        power_of_two : bool, optional, default=True
            if True the given dummy_member value will be squared.

        '''
        self.dummy_member = dummy_member

        if time_ten:
            self.dummy_member = 10. * self.dummy_member

        if multiplicator is not None:
            self.dummy_member = multiplicator * self.dummy_member

        if power_of_two:
            self.dummy_member = self.dummy_member * self.dummy_member

This can then be called like:

planete_keywords = {'time_ten': True,
                    'multiplicator': 25.,
                    'power_of_two': True }

planete = MySuperClass(120., **planete_keywords)

the * and ** operators transform a tuple and a dict into a list of parameters and keywords respectively.

def function_with_params(a, b, c):
    ...
    return(d)

can be called:

function_with_params(1., 2., 3.)

or in the same way

params = (1., 2., 3.)
function_with_params(*params)

the same for keywords:

def function_with_keywords(a, b, kwa=None, kwb=True):
    ...
    return(w)

can be called like

function_with_keywords(2., 3., kwa=12., kwb=False)

or that way:

params = (2., 3.)
keywords = {'kwa': 12.,
            'kwb': False}
function_with_keywords(*params, **keywords)
Assignee
Assign to
Fix all of Yori's comments.
Milestone
Fix all of Yori's comments. (Past due)
Assign milestone
Time tracking
None
Due date
None