[Tutor] How arguments to the super() function works?
Arup Rakshit
ar at zeit.io
Sun May 19 02:28:20 EDT 2019
> On 19-May-2019, at 4:46 AM, Mark Lawrence <breamoreboy at gmail.com> wrote:
>
> On 18/05/2019 17:21, Arup Rakshit wrote:
>> I am writing an Flask app following a book, where a piece of python concept I am not getting how it works. Code is:
>> class Role(db.Model):
>> __tablename__ = 'roles'
>> id = db.Column(db.Integer, primary_key=True)
>> name = db.Column(db.String(64), unique=True)
>> default = db.Column(db.Boolean, default=False, index=True)
>> permissions = db.Column(db.Integer)
>> users = db.relationship('User', backref='role', lazy='dynamic')
>> def __init__(self, **kwargs):
>> super(Role, self).__init__(**kwargs)
>> if self.permissions is None:
>> self.permissions = 0
>> Here, why super(Role, self).__init__(**kwargs) is used instead of super().__init__(**kwargs) ? What that Role and self argument is instructing the super() ?
>> Thanks,
>> Arup Rakshit
>> ar at zeit.io
>
> Please check this https://www.youtube.com/watch?v=EiOglTERPEo out. If that doesn't answer your question please ask again.
>
Hello Mark,
Thanks for sharing the link. Just finished the talk. Little better now about how super uses the MRO.
class Dad:
def can_i_take_your_car(self):
print("No...")
class Mom(Dad):
def can_i_take_your_car(self):
print("Asking your dad...")
class Victor(Mom, Dad):
def can_i_take_your_car(self):
print("Asking mom...")
class Pinki(Mom, Dad):
def can_i_take_your_car(self):
print("I need it today..")
class Debu(Pinki, Victor):
def can_i_take_your_car(self):
super(Victor, self).can_i_take_your_car()
In this piece of code:
print(Debu().can_i_take_your_car())
Why the above call prints "Asking your dad…” but not " print("Asking mom…”)” although can_i_take_your_car is defined inside the class Victor ?
help(Debu) #prints
Help on class Debu in module __main__:
class Debu(Pinki, Victor)
| Method resolution order:
| Debu
| Pinki
| Victor
| Mom
| Dad
| builtins.object
|
| Methods defined here:
|
| can_i_take_your_car(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Dad:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
Last question: Why duplicate PEPs for the same thing https://www.python.org/dev/peps/pep-0367/ and https://www.python.org/dev/peps/pep-3135/#specification ? Which one to read when such duplicate exists?
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
Thanks,
Arup Rakshit
ar at zeit.io
More information about the Tutor
mailing list