class Pregunta(object):
# método
def foo(self):
pass
# ¿Esto es una variable o tiene algun otro nombre?
bar = 'tijuana'
Are variables within a class given another name just like functions are called methods?
They are called attributes if they are declared and assigned a value in the same class.
Not to be confused with properties, which are a type of attribute, which are only declared in the class, but are not assigned a value in the same declaration (or are assigned a value of None), but are instead assigned a value in an instance of that class.
With the __get__ , __set__ , and __delete__ methods you can regulate the behavior of properties, and access to them from other classes.
In this article you can get a general idea of how to use attributes and classes in python.
In this other you can find more information about managing properties.
I hope it helps you.
good Andrew
If I'm not mistaken, they are called attributes, in the case you raise, the bar attribute of the Question object and it could be referred to as follows:
It is more fully explained in the documentation .
I hope this helps you
The nomeclature is fine to use to make ourselves understood, but if we were strict there would be no way to clarify.
Every object in python has an associated "dictionary" that can be viewed using the
vars
. Classes, as objects that they are, also have their dictionary and it would be formed with that "namespace" that is created when you define the class.The dictionary items of an object are the "attributes" , and those of a class are known as "class attribute" (<< this would be the short answer to your question ).
By function we mean a piece of code that takes values as input and returns a result as output. There is no such thing as "pure functions" in python . Whenever we define a function what we actually create is a "descriptor" object . With the descriptors the "methods" and "properties" are implemented , and they are responsible for the "inheritance" to work .
A method is defined as a piece of code whose instantiation creates closures that provide an execution context. Viewed another way, methods are classes whose instances are closures. To create methods, descriptors are assigned as class attributes.
...and I think I'm going to stop here.
They are global variables.
They can be used inside any method of the class where it is declared. If it is private, it can only be used by the class that declares it.
Example: