#๐ python beginner's question
53 messages ยท Page 1 of 1 (latest)
@dull axle
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.
they have a special meaning. init is the initializer to initialze the object
You can think of classes as a way to group data. Classes allows you to store variables and functions in a closed scope.
Dunder methods are special methods for classes which define some lower level methods, one of them is the __init__ also called the constructor method. This always gets fired once during instantiation of a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("baegy", 28)
# The Person("baegy", 28) is called the instantiation of the Person class. And now the variable person1 holds the created instance.
From the above example you can now access those variables from the instantiated class and also you can create endless number of instances of a Person class
print(person1.name, person1.age)
person2 = Person("Jiggly", 18)
print(person2.name, person2.age)
__init__ is usually used to create instance variables of the class
can that print command be placed above the class command?
oh oof wrong reply
can that print command be placed above the class command?
Wym
no python is interpreted not compiled
so it goes line by line
everything which is under the current line does not exists (yet)
thank you guys I think I understand the concept
and uhm
this might be another quite dumb question
but for the variable person1, can it also be written as either {} or [] instead of tuples?? why or why not?
You can ask you don't need to feel dumb about it
thanks ๐
Can you write an example
either person1 = Person["baegy", 28] or person1 = Person{"baegy", 28}
Yeah that's not a valid syntax
why?
you can call the class only with ()
something like Uniform Initialization does not exists
{} is a used to define a set or a dictionary, [] is used to define a list or index through a list / tuple or dictionary depending on how it's used, () is used to create a tuple or call a function (depending on how it's used again)
you get an new instance when you call the class object and calling in Python is always ()
ohh
I once made a brief post on classes and OOP here https://discord.com/channels/267624335836053506/1235883588206202942
Fyi, tuples and () are different ideas. Tuples just sometimes use parenthesis. ,s are actually what you typically need to create a tuple.
!e
x = 1, 2, 3
print(type(x))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
<class 'tuple'>
I don't have a permission of it
right
oh
Get the archived channel access
ok
!e
x = (1) # doesn't create a tuple
print(type(x))
y = (1,) # or y = 1,
# creates a tuple
print(type(y))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <class 'int'>
002 | <class 'tuple'>
Damn. thank you guys for the input my good sir ๐ท. may the rest of your day and ones to follow, be in your favour
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.
๐ python beginner's question