Problem coding Pyhton I need help

class Person:
def __init__(self, name):
self.name = name

def greet(self, other_name):
return "Hi {0}, my name is {1}".format(other_name, name)

Other urls found in this thread:

scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design
docs.python.org/3/tutorial/classes.html
twitter.com/SFWRedditGifs

why do python devs have a fetish for making everything a class

1) 2) use code brackets, retard

object oriented tendencies?

this isn't your tech support team
go to stackoverflow

this is like asking why haskell devs have a fetish for functional programming. it's an important feature of the language; if you didn't want to make things into classes, you shouldn't have started with python in the first place.

I've literally never found a use for classes in python, and I've made a ton of things with it

brackets shit,

Good job bypassing great programming principles and paradigms.

Golang is calling you.

you would have to be a literal retard if you've worked with python very much and *literally* never found a suitable use case for classes.

maybe you've been using the wrong language all this time if none of your problems could benefit from the creation of your own classes. i don't know how that would be much better, but there's one alternative, i guess.

no, the language is built around object-oriented design. to voluntarily not use classes *in python* is to walk around with your head up your ass.

Everything in Python is an object, which is an instantiation of a class, so you've used classes no matter what.

Either you didn't appreciate this (in which case you've clearly not worked with Python very much) or you meant that you've never made your own classes, which a seasoned developer should know sufficiently well to differentiate, or ... I don't know.

They're completely unecesary and messy as fuck with all the __doubleUnderscores__ and self.crap.

I just can't think of any context where I would need a class

exactly sempai

this

I don't even comphened what it means,
can somebody teach me python classes right nao?

that's pretty much what i said, unless you're attacking the nuance between being a retard and having one's head up their ass.

to say nothing of the fact that everything in python is a subclass of the Object class, and every method is just you calling an inherited method from an object you instantiated. like if you said
example="niggers"
example.upper() # "NIGGERS"

you made a string object - an element of class `str` - and executed one of its (many) methods.

scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design

use this paceholders and stip being a faggot
%s

init: a factory that provides you with a workable object initialized to your liking.

.self vs not .self: instance methods vs class methods.

ANY LANGUAGE THAT DOES NOT USE BRACKETS IS FOR FAGGOTS REEEEEEEE

isn't there a place on Sup Forums for autistic kids to stomp around without being triggered?

example?

It should be .format(other_name, self.name) and not .format(other_name, name)

like this:

class Person:
def __init__(self, name):
self.name = name

def greet(self, other_name):
return "Hi {0}, my name is {1}".format(other_name, self.name)

docs.python.org/3/tutorial/classes.html

so you use classes where a function would work perfectly well? fucking stupid.

I don't understhand that self shit
why is "self" a parameter

how would one create a Person object?

self basically tells Python that the variable "name" is inside the class

and this is a good opportunity to look at default arguments
class Person:
def __init__(self,name):
self.name=name

def greet(self,otherName=""):
return "hello{}, my name is {}".format(" {}".format(otherName) if otherName else "",self.name)


a bit messy, but shows falling back to no entry.

guys, I come from haskell and how it works is that classes are typeclasses, meaning types can be instances of classes
e.g., classes have methods and types that support those methods can be instances

is this approach bad or what?

Classes are for groups of functions. Classes collect a group of functions that have a relationship of some sort that belong together.

Particularly, in large systems where inheritence comes into play, you want to be able to use abstraction at the right moments.

are you honestly this fucking retarded? and you're trying to get people to believe you know anything about python?

you sound like a first-year undergrad who's flunked an introductory python course.

that's honestly just fucking stupid? gets me angry honestly. really user you should just fuck off.

get out of my thread. fucking useless faggot.

Writing good code should never just be about "this works perfectly well". It should be about "this is organized, works well, and can be approached by other people".

everyone watch out we've got an angry autistic kid. might be armed. stay away from schools for the next few days.

>I can't think of any context where I would need to encapsulate data structures and methods operating on them
It's official: Python has superseded PHP as a go-to language for cluelss idiots.

Your insufferable attitude leads me to believe you don't have to work with other people. Delete your thread if you're so upset.

I've also never used a class in php desu

PHP is (still) the go to language for clueless web developers. Python has just emerged as the go to language for clueless developers on local machines.

using python for web dev is still just hard enough for idiots that they just fall back to php (or pick up ruby)

This is a troll

yeah that's obvious. someone is going to bite though.

no I'm being serious, my entire blog with an account system doesn't use a single class, I just don't get the need

The problem is these languages are un-opinionated and people who don't understand web development aren't going to understand the power in expressiveness.

>my entire blog with an account system

ENTIRE BLOG WITH AN ACCOUNT SYSTEM CONFIRMED

Than it is massively spaghettifyed

when to use classes?
give me a scenario that showcases the power of classes

thanks sempai

not really, I have functions for similar stuff(e.g. blog posts and comments, those use the same function I just pass different database stuff to them) I mean it's by no means GOOD, I just don't see the need for classes in such a thing.

HNGGGGGGGGGGGGGG!!!!!!!!!!!!!

HHHHNNNNNNGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

FUKKKKKKKKKK!!!!!!!!!!

IM.......................CURRENTLY............

TAKINGGGGGGGGGGGGGGG!!!!

AAAAAAAAAAAAAAAAA HUUUUUUUUUUUUUUUUUUGEEEEEEEEEEEEEEEEEEEEEEEE

SHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

introduce a new feature every week for a year

stop being vague
give me detailed scneatio

class Person:
def __init__(self, name):
self.name = name

def greet(self, other_name):
return "Hi {0}, my name is {1}".format(other_name, self.name)

p = Person("Faggot");
print("OP is a " + p.greet("user"));

Sure. Consider the case of writing your own ORM, should you ever need to.

Consider a scenario where you have two tables, 'accounts' and 'organizations'.

Now, say you want to regularly access information about organizations and accounts.

Would you create one large object, with dozens of methods to query the database, or would you make two objects, each pertaining to a specific table?

no I'd just make a function that queries the database for each table then use that returned information. still doesn't explain why I need a class.

consider me telling you to fuck off
sick of this vague shit

wait, so is ORM just mixing two types in one?

Now, say that each account has its own billing methods, which have to do with deposits and withdrawals. In this scenario, I would like to isolate that functionality to an account. Creating an Account class that initializes with its ID on the table allows you to do the following:

class Account:
def __init__(self, id):
self.id = id

def deposit(amount):
...etc,

notice how I don't have to keep typing ID, as the object was instantiated with a self.id? That's more DRY, and easier to extend when I want to add more methods pertaining to the account table.

Ok

I think you're still thinking small-picture here. What if I want to make various types of queries, like add, delete, get_all_billing_info? I want more methods and I want methods that don't require I write an entire query every time. I want to know the query performed by just looking at the method name. I am your coworker and I don't live inside of your head.

No - an ORM (object relational mapping) maps an object to its table in the database. That way, each class in an ORM pertains to just one table, and in the presence of hundreds of tables, you don't have a clusterfuck of methods.

speak in laymans pls

how can one have a set of methods which apply to more than one type and yet not be defining the methods for each type?

Sorry, I think I confused you. The methods don't apply to more than one type.

You split groups of methods based on their lack of relationship. For instance, get_all_account_data would make no sense living anywhere but on an object that has multiple accounts under its hierarchy. Furthermore, get_all_organization_data would not belong anywhere except on an object that belongs to or owns one organization (in this case, belongs to organization). These two have no relationship with each other but definitely have relationships/dependencies with specific concepts (the organization and the account). Is this making sense, or am I being too vague?

The logic in object-oriented design is sort of vague by definition. A very simple set of methods may not need any class. But as a system grows in complexity, patterns reveal themselves and groups of things seem to make sense being kept in a discrete grouping (class).

Let me be more clear: the point at which you decide to group things into a class is not always very easy to distinguish.

what forumla do you use to express the relationship??

It really, really depends on the situation. I always ask myself, "where does this belong? Is there any other place in this system where very similar functionality is present? Would it make sense to group these together?" As you get more experienced with object-oriented programming, the questions just become part of your instinct. I highly recommend getting to know SOLID, because it'll help you answer those questions. Sandi Metz' talks on object-oriented design are also great for learning about classes. She is a champion in the world of OOP.

Additionally, this SHOULD be kind of difficult to understand. It's a taste of applied computer science.

kind of hard to take a woman seriously with something like oop
do you know any other authors male?

Bye

faggot

>you sound like a first-year undergrad who's flunked an introductory python course.

Sorry old man but nowadays they teach programming in elementary school.

2.x version. go fuck yourself

Self is like 'this' in C++, you reference yourself as an object. Whats hard about that? Its basically implicit in its name.

>using 2.x
Go fuck YOURSELF.

so what's your explanation for not understanding this shit? that you got held back in elementary school/

Classes are helpful when you are trying to implement layers of abstraction as a project grows.

Had to write a small elevator sim. Started with a class that handles just movements and actions of an elevator. Basic methods like move up, move down, open door, close door, get floor etc.

The actual simulation logis is written in a higher class which creates its own instance of an elevator logic.

Eventually i make a menu class that will be the only thing called in the main basically. It will give you options to run the sim, play w the elevator manually, or exit.

Classes keep the code extremely orginized and neat and extensible. Everything has its own cpp file and header file.

...

Sandi pls go

He fell for the stale meme.

Not that user just saying you don't have to be in college to know basic level of programming.

The user arguing against classes in python is a complete retard, is what I think btw.

>using old-style classes that don't inherit from object
kill yourself

use self.name instead of name

this has been brought up please ctrl-f the thread next time

self.name not name you dipshit
on the last line

shut up faggot

I'm good at python and I don't understand what you are trying to do with that return statement.

return "Hi " + other_name + ", my name is " + self.name Is what you should have done.

Why the /FUCK/ would you use .format to concat a space infront of a string?

and what the hell were you thinking adding a non-positional argument just to check if it was used or not? Just In case you want to call the method without a string?

Utterly retarded.

> __init__
This language is such a mess.

> he doesn't know anything about a language so he picks on the syntax

Fuck off, it's more clear than java and c++ constructors anyway.