Python Forum

Full Version: python class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

my question maybe is very simple but I am beginner with Python. What does this line do ?
return Post(self, message)

because I am confused: class Post is an object with two args (author and message) et just one arg is used !
Thanks in advance.

import crypt
import datetime
 
class User:
    def __init__(self, id, name, password):
        self.id = id
        self.name = name
        self._salt = crypt.mksalt()
        self._password = self._crypt_pwd(password)
 
    def _crypt_pwd(self, password):
        return crypt.crypt(password, self._salt)
 
    def check_pwd(self, password):
        return self._password == self._crypt_pwd(password)
 
    def post(self, message):
        return Post(self, message)
 
class Post:
    def __init__(self, author, message):
        self.author = author
        self.message = message
        self.date = datetime.datetime.now()
 
    def format(self):
        date = self.date.strftime('le %d/%m/%Y à %H:%M:%S')
        return '<div><span>Par {} {}</span><p>{}</p></div>'.format(self.author.name, date, self.message)
(Aug-18-2018, 07:39 AM)chris_thibault Wrote: [ -> ]What does this line do ? return Post(self, message)
This line creates a new Post instance with the user 'self' as author and returns it as the result of the User.post() method. Note that the 'self' in this call is not the same as the 'self' in the Post.__init__ method, which is the Post instance itself.
Thank you for your clear answer :)