Is the class (method?) just to customize a new type? Is an instance just a particular variable of a new customized type? Really confused about "attribute" because there are many different opinions like here and here.
class PartyAnimal:
x = 0
def party(self) :
self.x = self.x 1
print("So far",self.x)
an = PartyAnimal()
btw how's the self.x = self.x 1
work here?
I appreciate your help and pardon me for the broken English.
CodePudding user response:
Is the
class
(method?) just to customize a new type?
A class
is the framework for an object. An object is an instance of the class
:
class PartyAnimal:
...
# `an` is an instance of PartyAnimal
an = PartyAnimal()
A method is a function within the class
. In your example, def party(self)
is a method of PartyAnimal
Really confused about "attribute"
An attribute of a class
is the class.something
thing.
The attribute of your PartyAnimal
class
is the self.x
part, where x
is an attribute of PartyAnimal
. You can get the value of x
for each instance with:
class PartyAnimal:
x = 0
def party(self) :
self.x = self.x 1
print("So far",self.x)
>>> an = PartyAnimal()
>>> an.x
0
# Then run the method
>>> an.party()
'So far 1'
>>> an.x
1
Docs - https://docs.python.org/3/glossary.html#term-attribute
btw how's the
self.x = self.x 1
work here?
You're adding 1
to the attribute x
. Same as x = 1
if you weren't using classes
I recommend reading the w3 article on classes. It's really good
CodePudding user response:
Putting in very easy words, any variables that are defined inside a class are called attributes. An instance of a class is a copy of that particular class.
When you instantiate a class PartyAnimal()
, a copy of it is created. You assign it to a variable of the same type an
.
an = PartyAnimal()
Now, let's refer to an
as an instance of the class PartyAnimal()
. an
contains an attribute x
which is initially set to the value 0
. It can be accessed using the .
operator.
print(an.x) # output: 0
By default, attributes of a class in python are public
. Meaning they can be accessed outside of the class. If you want to increment the value of x
by 1
, you can do it by:
an.x = an.x 1
print(an.x) # output: 1
Now, talking about the method party()
, which takes the parameter self
. self
represents the instance of the class. In party()
, the value of x
is incremented. To know that we are referring to the variable x
of that instance and not any other x
(sometimes the variable with the name x
is passed into the function), we use self
.
Refer to this to know more about self
.
CodePudding user response:
The first link you posted explains most of your questions...
Explaining what methods are - they're functions inside the object and class:
An object can contain a number of functions (which we call methods)
The second link you posted confirms this:
Method: A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self). See function and nested scope.
Explaining what classes are...
the
class
keyword defines a template indicating what data and code will be contained in each object of typePartyAnimal
. The class is like a cookie cutter and the objects created using the class are the cookies.
counts = dict()
Here we instruct Python to construct an object using thedict
template (already present in Python), return the instance of dictionary, and assign it to the variable counts.
It compares dict
and PartyAnimal
as both templates, both types of objects.
Explaining what attributes are - they're data inside the object and class:
We call data items that are part of the object attributes.
Attribute: A value associated with an object which is referenced by name using dotted expressions.
Explaining that object and instance are interchangeable and that they are assigned to variables...
an = PartyAnimal()
This is where we instruct Python to construct (i.e., create) an object or instance of the classPartyAnimal
.
Python constructs the object with the right data and methods and returns the object which is then assigned to the variable
an
.
When the
PartyAnimal
class is used to construct an object, the variablean
is used to point to that object.
Explaining your question about self.x = self.x 1
...
When the
party
method is called, the first parameter (which we call by conventionself
) points to the particular instance of thePartyAnimal
object thatparty
is called from. Within theparty
method, we see the line:
self.x = self.x 1
This syntax using the dot operator is saying "the
x
withinself
". Each timeparty()
is called, the internalx
value is incremented by 1 and the value is printed out.
I could not find conflicting information between the two links you posted.