0

New to python and have been working on improving my skills overall, however, I struggle with understanding classes and functions.

Why can or can't I do the following code below

class Person():
    name = 'Tom'
    age = 31
    has_job = False

Person.name = 'Tom'
Person.age = 31
Person.has_job = False  
print(Person.name, Person.age, Person.has_job)

compared to this

class Person():
    def __init__(self, name, age, has_job):
        self.name = name
        self.age = age
        self.has_job = has_job

    
p1 = Person('Tom', 31, False)

Is this just bad practice or is it something else entirely?

3 Answers 3

1

I don't think that writing a class like your first example would be very usefull, because the attributes remain the same for each instance. That means that every Person will be called by default 'Tom', will have the age: 41 and "has_job" will be set to false.

In the second example you've got a specific constructor that will initialise those variables and that's going to be more usefull. There's only one problem: you forgot to put ":" after def __init__(self, name, age, has_job) .

Also be aware of the indentation.

Your code should look like this:

class Person():
    def __init__(self, name, age, has_job):
        self.name = name
        self.age = age
        self.has_job = has_job
 
    
p1 = Person('Tom', 31, False)
print(p1.name);
Sign up to request clarification or add additional context in comments.

6 Comments

Whoops, I did forget to add that at the end of my code. I had indentation too although I wasn't so sure as to how I add this. My bad I was being sloppy about it. So If I get this correct the first example I gave is not creating a unique Person Object rather it would be static for the types of variables I pass though?
I'd say, if you would like to represent a person, than each person should have a specific age. When you create a person, then that object is already initialised with the values you specified when creating the class. Of course you have the option to modify those values, after creating the object: I mean let's say you create the person p1, using your first example. p1 is now called 'Tom' ... by default. You may change this by saying p1.name='John', but it would be much easier to directly intialise the attributes of a person, using a constructor, like it is being displayed in the second exemple.
Also if you would like to modify the attributes of an object of the class I recommend using getters and setters, to avoid 'giving public' access to those variables
Ah, I see so I could also do something like p1.age = int(47) or p1.has_job = True I am starting to understand things a bit more clearly. Now I would do this below the object when created? Unlike the first example where everything is set to a default value.
First, there's no need to specify int(47), p1.age = 47 is enough.
|
0

Python is white space sensitive. Unless you want to change the default values in you class you do not need to redefine them.

class Person():
    name = 'Tom'
    age = 31
    has_job = False
'''
change these will change the class values
Person.name = 'Tom'
Person.age = 31
Person.has_job = False  
'''
print(Person.name, Person.age, Person.has_job)

1 Comment

Oh, I kind of understand I thought I had to define them twice I guess I still need more practice.
0

In the first section of your code you are trying to define class attributes. These are attributes that do not change between instances of your class. On the other hand if you define variables in the def init(self) method these are parameters you must pass when creating the class and will be unique to each instance of the class you create. These are called instance attributes.

class Person():
   # these are class attributes.
   name = 'Tom'
   age = 31
   has_job = False

class Person2():
   def __init__(self, name, age, has_job)
    # these are instance attributes
    self.name = name
    self.age = age
    self.has_job = has_job

In your first code snippet you did not indent the classes attributes appropriately when you created the class. Check my example above to see how that would be done.

So in your case since each person will be a new instance of your Person class, you do not want to have name, age and has_job as class attributes since those are unique to every person you create. If you had those variables as class attributes then each person you create using your Person() class will have the same name, age, and has_job values.

If you created a class with class attributes and then changed the class attributes of the class instance every time it would not be pythonic. Rather you should create instances of the class with instance attributes.

I HIGHLY recommend watching Corey Shafer OOP tutorials on youtube as they cover all this extensively: https://www.youtube.com/watch?v=ZDa-Z5JzLYM&list=PL-osiE80TeTt2d9bfVyTiXJA-UTHn6WwU&index=40

1 Comment

This was really insightful. I'll definitely go check out the video. I had indentation but when I had pasted the code from notepad ++ I guess It did not port them? I am sort of understanding what is happening although with more time and practice I am surely able to understand what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.