Skip to main content
deleted 2 characters in body
Source Link
Matias Cicero
  • 26.5k
  • 21
  • 91
  • 161

You should take a look at the property class. Basically, it lets you encapsulate behaviour and private variablesmembers without the consumer even noticing it.

In your example, you may have a goalie_pulled property:

class Team(object):
    def __init__(self, team_name, tri_code, goals, shots, goalie_pulled):
        # Notice the identation here. This is very important.
        self.team_name = team_name
        self.tri_code = tri_code
        self.goals = goals
        self.shots = shots

        # Prefix your field with an underscore, this is Python standard way for defining private members
        self._goalie_pulled = goalie_pulled

    @property
    def goalie_pulled(self):
        return self._goalie_pulled

    @goalie_pulled.setter
    def goalie_pulled(self, new_value):
        self._goalie_pulled = new_value
        goalie_pulled_tweet(self) #self is the current Team instance

From the consumer's point of view:

team = create_team_instance()

# goalie_pulled_tweet is called
team.goalie_pulled = 'some_value'

I'd recommend you to use properties whenever you can (and must), as they are a nice way of abstraction.

You should take a look at the property class. Basically, it lets you encapsulate behaviour and private variables without the consumer even noticing it.

In your example, you may have a goalie_pulled property:

class Team(object):
    def __init__(self, team_name, tri_code, goals, shots, goalie_pulled):
        # Notice the identation here. This is very important.
        self.team_name = team_name
        self.tri_code = tri_code
        self.goals = goals
        self.shots = shots

        # Prefix your field with an underscore, this is Python standard way for defining private members
        self._goalie_pulled = goalie_pulled

    @property
    def goalie_pulled(self):
        return self._goalie_pulled

    @goalie_pulled.setter
    def goalie_pulled(self, new_value):
        self._goalie_pulled = new_value
        goalie_pulled_tweet(self) #self is the current Team instance

From the consumer's point of view:

team = create_team_instance()

# goalie_pulled_tweet is called
team.goalie_pulled = 'some_value'

I'd recommend you to use properties whenever you can (and must), as they are a nice way of abstraction.

You should take a look at the property class. Basically, it lets you encapsulate behaviour and private members without the consumer even noticing it.

In your example, you may have a goalie_pulled property:

class Team(object):
    def __init__(self, team_name, tri_code, goals, shots, goalie_pulled):
        # Notice the identation here. This is very important.
        self.team_name = team_name
        self.tri_code = tri_code
        self.goals = goals
        self.shots = shots

        # Prefix your field with an underscore, this is Python standard way for defining private members
        self._goalie_pulled = goalie_pulled

    @property
    def goalie_pulled(self):
        return self._goalie_pulled

    @goalie_pulled.setter
    def goalie_pulled(self, new_value):
        self._goalie_pulled = new_value
        goalie_pulled_tweet(self) #self is the current Team instance

From the consumer's point of view:

team = create_team_instance()

# goalie_pulled_tweet is called
team.goalie_pulled = 'some_value'

I'd recommend you to use properties whenever you can (and must), as they are a nice way of abstraction.

Source Link
Matias Cicero
  • 26.5k
  • 21
  • 91
  • 161

You should take a look at the property class. Basically, it lets you encapsulate behaviour and private variables without the consumer even noticing it.

In your example, you may have a goalie_pulled property:

class Team(object):
    def __init__(self, team_name, tri_code, goals, shots, goalie_pulled):
        # Notice the identation here. This is very important.
        self.team_name = team_name
        self.tri_code = tri_code
        self.goals = goals
        self.shots = shots

        # Prefix your field with an underscore, this is Python standard way for defining private members
        self._goalie_pulled = goalie_pulled

    @property
    def goalie_pulled(self):
        return self._goalie_pulled

    @goalie_pulled.setter
    def goalie_pulled(self, new_value):
        self._goalie_pulled = new_value
        goalie_pulled_tweet(self) #self is the current Team instance

From the consumer's point of view:

team = create_team_instance()

# goalie_pulled_tweet is called
team.goalie_pulled = 'some_value'

I'd recommend you to use properties whenever you can (and must), as they are a nice way of abstraction.