Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python class
#1
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)
Reply
#2
(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.
Reply
#3
Thank you for your clear answer :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting c++ class to python class panoss 12 11,709 Jul-23-2017, 01:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020