Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class arguements
#1
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 ?
Reply
#2
I suggest you go through this site:
https://docs.python.org/3/tutorial/classes.html
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
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):
    pass
But basics of inheritance is explained in the link I shared in the other thread already.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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 week
how do you call lines 4 to 9 ? (like, is there a term for it ?)
Reply
#5
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.
Reply
#6
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__()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
so it basically an initialization ? like starting-up a car ? / loading up the initial parameters ?
Reply
#8
(Apr-29-2020, 04:16 PM)astral_travel Wrote: so it basically an initialization ?
yes
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing varying command line arguements Rakshan 3 2,008 Jul-28-2021, 03:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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