How can I define a class in Python?

OscarRyz :

Quite simple, I'm learning Python, and I can't find a reference that tells me how to write the following:

public class Team {
     private String name;
     private String logo;
     private int members;

     public Team(){}

     // Getters/setters
}

Later:

Team team = new Team();
team.setName("Oscar");
team.setLogo("http://....");
team.setMembers(10);

That is a class Team with the properties: name/logo/members

Edit

After a few attempts I got this:

class Team:
    pass

Later

team = Team()
team.name = "Oscar"
team.logo = "http://..."
team.members = 10

Is this the Python way? It feels odd (coming from a strongly typed language of course).

Martin v. Löwis :
class Team:
  def __init__(self):
    self.name = None
    self.logo = None
    self.members = 0

In Python, you typically don't write getters and setters, unless you really have a non-trivial implementation for them (at which point you use property descriptors).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I define a containing class for another class in python?

How can I define a class in multi file(or module) in python

How can I define multiple class objects with a for loop in python?

How can I define dynamic class member?

How can i define a method in a class that it return only part of the class

How can I define multidimensional arrays in python?

how i can define a function in python 3

How can i call extra method that i define in anonymous class?

How to define "in" for a python class

How can i define a class to deserialze binary file

How can I define an class' array while constructing it?

How can I define a method inside a class component in React?

How can I define a struct in (*.cpp) instead (*.h) in a class

Can't define class in python

How should I define a __repr__ for a class hierarchy in python?

How can I define a TypeAlias for a nested Generic in Python?

How can I define a Nix environment that defaults to Python 3.5

How can i define a shared list in the recursive function in python?

How I can define function to integrate lists using python?

How can I define a function called "import" in Python?

How can I define a javascript variable on a webpage in selenium using Python?

How can I define default values using context manger in Python

How to define a class in Python 2?

How can I define such an array?

Can I define/link types to a class in typescript?

Can I define custom character class shorthands?

Can I define trait's variable in class?

How can I define in LESS an element that has a class of class="btn btn-primary"

How do I define implicit class that can pimp both base and derived class

TOP Ranking

HotTag

Archive