Python Forum
Hi, need help with class, object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hi, need help with class, object
#1
class MyRouter(object):
 "This is a class that defines the aspects of a router"
 def _init_(self, routername, model, serialno, ios):
    self.routername = routername
    self.model = model
    self.serialno = serialno
    self.ios = ios

 def print_router(self, manuf_date):
    print("The router name is: ", self.routername)
    print("The router model is: ", self.model)
    print("The serial number of: ", self.serialno)
    print("The IOS version is: ", self.ios)
    print("The model and date combined: ", self.model + manuf_date)

 router1 = MyRouter("h1", "starhub", "123", "155")
 print(router1)
Hi , when i am trying to print out router 1 , it returns an error message below :

Error:
Traceback (most recent call last): File "C:/Users/teoho/PycharmProjects/untitled4/try1.py", line 1, in <module> class MyRouter(object): File "C:/Users/teoho/PycharmProjects/untitled4/try1.py", line 16, in MyRouter router1 = MyRouter("h1", "starhub", "123", "155") NameError: name 'MyRouter' is not defined
Why is that so ? Many thanks in advance.
Reply
#2
the indentation is not correct:

class MyRouter(object):
    "This is a class that defines the aspects of a router"
    def __init__(self, routername, model, serialno, ios):
        self.routername = routername
        self.model = model
        self.serialno = serialno
        self.ios = ios
    
    def print_router(self, manuf_date):
        print("The router name is: ", self.routername)
        print("The router model is: ", self.model)
        print("The serial number of: ", self.serialno)
        print("The IOS version is: ", self.ios)
        print("The model and date combined: ", self.model + manuf_date)
 
router1 = MyRouter("h1", "starhub", "123", "155")
print(router1)
Also, it's __init__, not _init_
finally, probably you want to implement __str__ method (e.g. instead of print_router

Note, strongly advise you to use 4 spaces per level of indentation, not 1.
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
  Printing out incidence values for Class Object SquderDragon 3 275 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  error in class: TypeError: 'str' object is not callable akbarza 2 502 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,863 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  AttributeError class object has no attribute list object scttfnch 5 3,420 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  Python Class and Object Parameters JoeDainton123 5 2,889 Sep-02-2020, 10:43 AM
Last Post: JoeDainton123
  Multiprocessing, class, run and a Queue Object SeanInColo 0 1,535 Jul-12-2020, 05:36 PM
Last Post: SeanInColo
  class returns NoneType Object istemihan 0 2,243 Aug-12-2019, 11:47 AM
Last Post: istemihan
  Object and type class Uchikago 2 2,227 Jul-28-2019, 10:35 AM
Last Post: DeaD_EyE
  Return a value when I equal an object from a class ihouses 4 2,971 Jul-10-2019, 02:44 AM
Last Post: SheeppOSU
  How to save a class object to a file? SheeppOSU 2 3,738 Jun-22-2019, 11:54 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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