Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class question
#1
Hello i have been learning Python 3 and its going in quite well.

But i have a query about classes.

i have the below code

class Student(object):
    def __init__(self, name="Rob", grade='A', county="Essex"):
        self.name = name
        self.grade = grade
        self.county = county

    student1 = Student()
    print(student1.name)
when i run in pycharm, i get the following error

Error:
student1 = Student() NameError: name 'Student' is not defined
can someone point out what im doing wrong please.

Kind Regards

Rob
Reply
#2
You need to either put student1 = Student() in the function or take it out of the class. You have it aligned with your function.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You have referred to the class inside the class definition. Modify your indents to do it outside.

class Student(object):
    def __init__(self, name="Rob", grade='A', county="Essex"):
        self.name = name
        self.grade = grade
        self.county = county

student1 = Student()
print(student1.name)
Output:
Rob
Reply
#4
Check your indentation @robdineen - the last two lines are inside the function
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
#5
(May-28-2020, 09:27 PM)bowlofred Wrote: You have referred to the class inside the class definition. Modify your indents to do it outside.

class Student(object):
    def __init__(self, name="Rob", grade='A', county="Essex"):
        self.name = name
        self.grade = grade
        self.county = county

student1 = Student()
print(student1.name)
Output:
Rob

Thank you very much, stupid error, so thank you for answering.
Reply
#6
Another one if this is even helpful

class Student(object):
    def __init__(self, name, grade, county):
        self.name = name
        self.grade = grade
        self.county = county

    def getName(self):
        return self.name

    def getGrade(self):
        return self.grade

    def getCountry(self):
        return self.county

    def __str__(self):
        return 'Name: '+ self.name + '\ngrade: ' + self.grade + '\ncountry: ' + self.county


def main():
    student1 = Student('Rob', 'A', 'Essex')
    print(student1)

if __name__ == '__main__':
    main()
Output
Name: Rob
grade: A
country: Essex
Reply
#7
@Calli:
OP question was ansered already.
What you suggest - using getters/setters for properties is anti-pattern in python. Even in your code you don't use them in __str__() method :-)
some reading: Why don't you want getters and setters?
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
#8
Just learning as much as i can thank you so much @buran i will do more reading on your link.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about __repr__ in a class akbarza 4 530 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
  Initiating an attribute in a class __init__: question billykid999 8 1,248 May-02-2023, 09:09 PM
Last Post: billykid999
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  newbie question....importing a created class ridgerunnersjw 5 2,570 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Question about naming variables in class methods sShadowSerpent 1 1,961 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  class question jrauden 1 1,529 Feb-16-2020, 10:14 AM
Last Post: ibreeden
  Class Question esteel 5 4,012 May-22-2018, 10:27 AM
Last Post: esteel
  First Post/ Class Error Question jvan1601 2 3,370 May-01-2017, 04:21 AM
Last Post: jvan1601

Forum Jump:

User Panel Messages

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