#πŸ”’ When doing classes, why not just do print?

50 messages Β· Page 1 of 1 (latest)

solemn parrot
#

I'm a bit confused with this task I have to implement objects into my class. In my tutorials, they just say "do this this this then boom, magic" (from what I understood)

Why then do I have to do in like lets say, email, from my understanding in the tutorials:

class Counselor: 
    def __init__(self,first,last,schedule):
        self.first = first
        self.last = last
        self.email = first + '.' + last + '@gmail.com'
        self.schedule = schedule

Counselor_1 = Counselor(first = 'Casey', last = 'Scout',schedule ='Mon-Fri 2-5PM')


print("Email:", Counselor_1.email)    

Whereas in my teachers tutorial which is after the video tutorial from YT I followed for lets say focusing on the email it is doing:

def __init__(self,id_number,name,degree_program, birthday):

        self.email_address = None

def get_email(self):
      return self.emailaddress

if  _ _ name _ _ ==  "_ _ main _ _"
      student1 = Student(name = John Doe, etc., etc. etc.,)

#then in for email it's just

student1.set_email("[email protected]")
print("Email address: ", student1.get_email() )
wispy starBOT
#

@solemn parrot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vapid zodiac
#

it's reusable

#

its a template

#

thus you can create lots of objects with the template

#

and reduce repetition

nova fable
#

I'm assuming there's a set_email method you've omitted.

In core OOP the attributes of an object (an instance of a class) are things you don't touch, and instead use a setter (eg your set_email method) to change state and a getter (eg get_email) to read state.

But in Python we broadly just get at attributes directly. student1.email = .... to set it, and just student1.email to get it.

vapid zodiac
#

^ can confirm, getters and setters are not really used in python (unless they use __ naming)

nova fable
#

In the first example the .email_address is set during the init of the object.

#

In your second example it's overtly set when you call the set_email method.

#

!code

BTW, please edit your post to format the python code. It's been mangled (because discord messages are markDown, and some of the Pythoj code has been interpreted as formatting).

wispy starBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

nova fable
#

@solemn parrot ^^

solemn parrot
solemn parrot
solemn parrot
# vapid zodiac its a template

I only understand the above template. I am a wee bit confused on the bottom template that has thereturn and way more def __init__

#

Also idk what to do when there is:

#get_name() - I think I just need to use the 1st template?
#get_email() - I feel I can just do this using the 1st template but then seeing how the prof didn't use the one from the YT tutorial but rather the bottom template, I am feeling I might miss something.
#get_schedule() - 1st template is what I feel I should use
#update_schedule() - I think I should use the same concept for get_email. Will send it below

solemn parrot
#

So far this is what I've progressed :>

solemn parrot
red relic
#

You put def_name

#

Not def name

#

Therefore it is invalid syntax

solemn parrot
#

Oh! Mb haha

#

Was manually copy pasting cuz the examples were screenshots

#

So far, hopefully I'm going the right direction to the tasks requestedπŸ˜…

solemn parrot
#

.

tame turret
solemn parrot
#

This what I worked on so far but idk why it still won't run...

#

oh

#

the if and else weren't....

#

weren't aligned

#

aight now it's line 25 that's an error πŸ’€

#

nvm

#

parenthesis

#

darn

agile pecan
# solemn parrot
  1. you don't actually need to spell out each argument; the values get assigned in-order.
a = Counselor("Green", "Grass", "Random Date")

print( a.first )  # 'Green'
solemn parrot
#

tried fixing some stuff but I'm confused where and whether to use Counselor or Counselor_1

agile pecan
#

!eval -
2) you'd need to call the function name() not just mention it.

I mean -

def name(something):
    print( f"{something} world!")

see the difference -

print( name )
print( name("Hello") )
wispy starBOT
solemn parrot
#

Ow

#

I need to like

#

hm

solemn parrot
#

hm

#

!close

wispy starBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.