Calling attributes of an object in another class in python

3

I'm a beginner in python and I wanted to know how I call attributes of another object in another class, because in Java, for example:

package model;
import java.util.ArrayList;
import java.util.List;

public class FuncionarioDAO {
    Produto p;   // Essa linha... como faço isso em python?


    List<Produto> itens = new ArrayList<Produto>();


    public void Cadastrar(Produto p) {
        itens.add(p);
    }

    public void Remover(int codigo) {
        for(int i=0; i<itens.size(); i++) {
            Produto p = itens.get(i);
                if(p.getCodigo() == codigo) {
                    itens.remove(i);
                    break;
                }
           }
      }

     public void Alterar(Produto p) {
         Remover(p.getCodigo());
         Cadastrar(p);   
         }
}

I also have another question, I have POO basis, but I do not quite understand the database interconnection with the application.

There is a time when I need to pass the data to SQL and I do not know what to do.

Could anyone give me tips?

    
asked by anonymous 17.07.2018 / 22:04

3 answers

2

For example in a file named person.py, you have the People class and want to call it in a main.py file

main.py

class Usuario:
    def __init__(self):
        self.pessoa = Pessoa()

If you want to call a function from your new file:

class Usuario:
    def minha_funcao(self):
        pessoa = Pessoa()
    
18.07.2018 / 22:27
1
  

I wanted to know how I call attributes of another object in another class

The line you have highlighted is declaring an attribute of the class. In Python, the statement is made in the first assignment. If you want to ensure that an attribute is created before it receives a specific value, declare it as None within __init__ .

class Teste():
    def __init__(self):
        self.p = None
  

I do not really understand database interconnection with the application.

This is very comprehensive to fit an answer.

  

Could anyone give me tips?

Search for SQLAlchemy tutorials.

    
18.07.2018 / 21:30
1

Look at the following example:

example.py

class Pessoa:

    def __init__(self, nome, idade):
        self.nome = nome
        self.idade = idade

    def setNome(self, nome):
        self.nome = nome

    def setIdade(self, idade):
        self.idade = idade

    def getNome(self):
        return self.nome

    def getIdade(self):
        return self.idade

To access the object, its attributes and methods, simply import it into the class you want:

main.py

from exemplo import Pessoa

pessoa = Pessoa('Roberto', '12')

pessoa.setNome('Paulo')
pessoa.setIdade('18')

print('O ' + pessoa.getNome() + ' possui ' + pessoa.getIdade() + ' anos.')


To learn more about imports follow this site . There are different ways to import a class or an object :

from modulo import objeto // Trará apenas o objeto requerido
from modulo import objeto as ob // Trará o objeto com o nome de 'ob'
from modulo import * // Importa todas as classes do módulo.


Your second question refers to data persistence using Python. For this, the most common way is to use ORM : Object-relational mappers, which are frameworks responsible for transposing sql tags into objects that can be used within the chosen language . Hibernate is an example of ORM for Java that is very well known in the middle. Already for Python one of the best known is SQL Alchemy .

Useful links:

18.07.2018 / 18:59