class arguements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: class arguements (/thread-26353.html) |
class arguements - astral_travel - Apr-29-2020 I've noticed - sometimes there is code like this: class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("John", 36) print(p1.name) print(p1.age)where class definition is left empty (class Person), and sometimes code like this: class Employee(object):where the class is given an arguement, what is the difference, when each of the variation is being used ? RE: class arguements - pyzyx3qwerty - Apr-29-2020 I suggest you go through this site: https://docs.python.org/3/tutorial/classes.html RE: class arguements - buran - Apr-29-2020 in python3 there is no difference. object is base class for all classes and it's not necessary to explicitly inherit from object .>>> class Foo: ... pass ... >>> type(Foo) <class 'type'> >>> class Bar(object): ... pass ... >>> type(Bar) <class 'type'>In python2 there are old-style (or classic) class and new-style class. For compatibility reasons, classes are still old-style by default. To be new-style class, it had to inherit from object explicitly.>>> class Foo: ... pass ... >>> type(Foo) <type 'classobj'> >>> class Bar(object): ... pass ... >>> type(Bar) <type 'type'>New-style and classic classes (Apr-29-2020, 10:03 AM)astral_travel Wrote: where the class is given an arguement, in terms of terminology object is not argument, it's a parent class from which your class inherits. Of course your class can inherits from other custom class, e.g.class Vehicle: pass class Car(Vehicle): passBut basics of inheritance is explained in the link I shared in the other thread already. RE: class arguements - astral_travel - Apr-29-2020 yea i'm reading it now (i read the first article, now i'm in the class inheritance article), and there's something that i see repeats itself always when defining a class: in this code: class AnyEmployee(object): def __init__(self, grade, title, first, last, location, phone, pay): self.grade = grade self.title = title self.name = (last, first) self.location = location self.phone = phone self.pay = pay def contact_info(self): text = ', '.join(self.name) + ' (' if self.location: text += self.location + ', ' text += self.phone + ')' return text def weekly_pay(self, hours = 0): if self.grade == 'Manager': return round(self.pay / 52, 2) else: week = self.pay * hours if hours > 40: week += self.pay * 0.5 * (hours - 40) return weekhow do you call lines 4 to 9 ? (like, is there a term for it ?) RE: class arguements - deanhystad - Apr-29-2020 They get called after the class is created. obj = AnyEmployee(grade, title, first, last, location, phone, pay) Will create an AnyEmployee object (allocate space and provide an object ID) and then call the __init__ method for that class passing along the arguments. There is also a __new__ method that gets called before the object is created. It could be used to make a singleton or some used with __del__ to implement a resource pool. You don't see it used very often. There are a bunch of dunder (double underscore) methods, sometimes called "magic" methods. RE: class arguements - buran - Apr-29-2020 lines 3-9 is the __init__() functions. That is one of the number of special methods, so called dunders (from double underscore) functions that can be used to customize class.__init__() is called at time of class instantiation (i.e. every time when you create an instance of the class).on lines 4-9 they initialize some class attributes. Note that although normally you would inirialize some if not all attributes in __init__(), it's not mandatory that all attributes are initializaed in __init__() . actually it's not mandatory to have __init__()
RE: class arguements - astral_travel - Apr-29-2020 so it basically an initialization ? like starting-up a car ? / loading up the initial parameters ? RE: class arguements - buran - Apr-29-2020 (Apr-29-2020, 04:16 PM)astral_travel Wrote: so it basically an initialization ?yes |