Where does the **get** in the below code come from?

In this lesson, it uses self.permissions.get(key), I want yo ask, where does the get attribute come from?

Code

class User:
  def __init__(self, username, permissions):
    self.username = username
    self.permissions = permissions

  def has_permission_for(self, key):
    if self.permissions.get(key):
      return True
    else:
      return False

.get() is method implemented by dictionary. Each data type has certain methods

https://www.programiz.com/python-programming/methods/dictionary

2 Likes

Yeah, thanks, I did not read through to see where it says ‘lets assume that ““key”” is a dict()’. I was about to come delete the question before I saw your reply. I will just leave it like so. Thanks.

You raise an interesting point. We now assume this.permissions is a dictionary. We don’t verify that anywhere.

key isn’t a dictionary, this.permissions is. key is, well, the key which we are going to lookup in the dictionary.

1 Like

I see. This makes more sense.

I have got another issue, though similar. This exercise uses some undefined atrributes (like message.sender and message.text) in the User class and the interpreter dose not return any error. And obviously, it does not inherent from the Message class. I tried the same code in my shell and it tells me that User has no such attribute. I even copied the Message class as well, still it retured the same error. Could you please look at it.

Code

class Message:
  def __init__(self, sender, recipient, text):
    self.sender = sender
    self.recipient = recipient
    self.text = text

class User:
  def __init__(self, username):
    self.username = username
    
  def edit_message(self, message, new_text):
    if message.sender == self.username:
      message.text = new_text

class Admin(User):
  def edit_message(self, message, new_text):
    message.text = new_text

    
  pass
print("hello")

The print(“hello”) is me trying to confirm if the interpreter at this stage was not turned off.
Thanks.

Similar issue indeed. I think its good practice for you to actually make the class instances and call the methods, so you know what values the parameters get.

Because do you currently now what value message parameter of edit_message method expects?

we can actually pass class instances as arguments to functions and methods, that is also what is happening here. For the message parameter you should pass an instance of Message class. This instance does have a .sender attribute

1 Like

I see, I tried the same thing initially, but I was sceptical because Message takes three params but edit_message takes two.

I passed to User the same object I instantiated Message with, then passed it to edit_message as an argument. I still got an error.

can i see this?

and why this a problem? If we have a function with a single parameter:

def example(par):
   print(par)

we can still pass a dictionary to the function:

example({1: 2, 3: 4})

the dictionary has multiple keys. Yet this isn’t a problem

same with class instance. The instance then can have multiple properties, but its still a single class instance. just like a dictionary is one dictionary with multiple key value pairs.

2 Likes

Below is my instantiation of Message and User.

message = Message("Me", "You", "We shall be good.")

me = User("Me")
me.edit_message(message, "Shall We?")


Yet, when I try to call message.text or .sender with these

me.message.sender
me.message.text

I get

#AttributeError: 'User' object has no attribute 'message'

I understand you, what I mean is, the class Message takes three arguement but User takes one and edit_message takes two, that got me confused.

Can I see how you will go about instantiating the code?

why would me get a message attribute all of a sudden? We pass this class instance to edit_message method:

  def edit_message(self, message, new_text):
    if message.sender == self.username:
      message.text = new_text

so that within this method, we access to properties of Message instance.

There is no magical adding of properties to the me variable (user class instance).

as we can see:

class Message:
  def __init__(self, sender, recipient, text):
    self.sender = sender
    self.recipient = recipient
    self.text = text

class User:
  def __init__(self, username):
    self.username = username
    
  def edit_message(self, message, new_text):
    if message.sender == self.username:
      message.text = new_text

class Admin(User):
  def edit_message(self, message, new_text):
    message.text = new_text

message = Message("Me", "You", "We shall be good.")

me = User("Me")
me.edit_message(message, "Shall We?")
print(message.text)

the text of the message has changed.

__init__() (which means initialize) is a magic method which is called when we create a class instance. So the argument we provided when create class instance are used to set the instance variable.

Each method (including __init__ can have as many parameters as we need.

1 Like

@stetim94 thank you for this. I can now see my failing.

1 Like