[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Problem coding Pyhton I need help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 84
Thread images: 6

File: opengraph-icon-200x200.png (8KB, 200x200px) Image search: [Google]
opengraph-icon-200x200.png
8KB, 200x200px
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)
>>
why do python devs have a fetish for making everything a class
>>
1) >>>/g/dpt/
2) use code brackets, retard
>>
>>55928925
object oriented tendencies?
>>
File: 1463747427423.jpg (37KB, 540x431px) Image search: [Google]
1463747427423.jpg
37KB, 540x431px
this isn't your tech support team
go to stackoverflow
>>
>>55928925
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.
>>
>>55928945
I've literally never found a use for classes in python, and I've made a ton of things with it
>>
>>55928928

brackets shit,
>>
>>55928956
Good job bypassing great programming principles and paradigms.

Golang is calling you.
>>
>>55928956
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.
>>
>>55928974
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.
>>
>>55928956
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.
>>
>>55928956
They're completely unecesary and messy as fuck with all the __doubleUnderscores__ and self.crap.
>>
>>55928974
>>55928972
I just can't think of any context where I would need a class

>>55928993
exactly sempai
>>
>>55928993
this

I don't even comphened what it means,
can somebody teach me python classes right nao?
>>
>>55928984
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.
>>
>>55929006
https://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
>>
>>55929020
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
>>
>>55929071
isn't there a place on /g/ for autistic kids to stomp around without being triggered?
>>
>>55929068
example?
>>
>>55928914

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)
>>
>>55929078
https://docs.python.org/3/tutorial/classes.html
>>
>>55929038
so you use classes where a function would work perfectly well? fucking stupid.
>>
>>55929083
I don't understhand that self shit
why is "self" a parameter

how would one create a Person object?
>>
>>55929107

self basically tells Python that the variable "name" is inside the class
>>
>>55929083
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?
>>
>>55929102
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.
>>
>>55929102
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.
>>
>>55929141
that's honestly just fucking stupid? gets me angry honestly. really anon you should just fuck off.

get out of my thread. fucking useless faggot.
>>
>>55929102
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".
>>
>>55929153
everyone watch out we've got an angry autistic kid. might be armed. stay away from schools for the next few days.
>>
>>55929006
>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.
>>
>>55929153
Your insufferable attitude leads me to believe you don't have to work with other people. Delete your thread if you're so upset.
>>
>>55929174
I've also never used a class in php desu
>>
>>55929174
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)
>>
File: 1464094837995.jpg (19KB, 280x400px) Image search: [Google]
1464094837995.jpg
19KB, 280x400px
>>55929180
This is a troll
>>
>>55929188
yeah that's obvious. someone is going to bite though.
>>
>>55929188
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
>>
>>55929184
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.
>>
>>55929205
>my entire blog with an account system

ENTIRE BLOG WITH AN ACCOUNT SYSTEM CONFIRMED
>>
>>55929205
Than it is massively spaghettifyed
>>
when to use classes?
give me a scenario that showcases the power of classes
>>
>>55929214
thanks sempai

>>55929218
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.
>>
>>55929235
>>55929218

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

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

FUKKKKKKKKKK!!!!!!!!!!

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

TAKINGGGGGGGGGGGGGGG!!!!

AAAAAAAAAAAAAAAAA HUUUUUUUUUUUUUUUUUUGEEEEEEEEEEEEEEEEEEEEEEEE

SHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>>
>>55929231
>>55929235
introduce a new feature every week for a year
>>
>>55929252
stop being vague
give me detailed scneatio
>>
>>55928914
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("Anon"));

>>
>>55929256
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?
>>
>>55929308
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.
>>
>>55929308
consider me telling you to fuck off
sick of this vague shit
>>
>>55929308
wait, so is ORM just mixing two types in one?
>>
>>55929308
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.
>>
>>55929319
Ok

>>55929318
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.
>>
>>55929355
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.
>>
>>55929391
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?
>>
>>55929424
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).
>>
>>55929469
Let me be more clear: the point at which you decide to group things into a class is not always very easy to distinguish.
>>
>>55929469
what forumla do you use to express the relationship??
>>
>>55929509
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.
>>
>>55929509
Additionally, this SHOULD be kind of difficult to understand. It's a taste of applied computer science.
>>
>>55929550
kind of hard to take a woman seriously with something like oop
do you know any other authors male?
>>
>>55929591
Bye
>>
>>55929623
faggot
>>
>>55929142
>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.
>>
>>55929282
2.x version. go fuck yourself
>>
>>55929107
Self is like 'this' in C++, you reference yourself as an object. Whats hard about that? Its basically implicit in its name.
>>
>>55929868
>using 2.x
Go fuck YOURSELF.
>>
>>55929743
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.
>>
File: 1449361855035.png (29KB, 826x392px) Image search: [Google]
1449361855035.png
29KB, 826x392px
>>55929153
>>
>>55929623
Sandi pls go
>>
File: Girls.jpg (77KB, 600x536px) Image search: [Google]
Girls.jpg
77KB, 600x536px
>>55929868
He fell for the stale meme.
>>
>>55930075
Not that anon just saying you don't have to be in college to know basic level of programming.

The anon 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
>>
>>55928914
use self.name instead of name
>>
>>55932714
this has been brought up please ctrl-f the thread next time
>>
>>55928914
self.name not name you dipshit
on the last line
>>
>>55932734
shut up faggot
>>
>>55929130

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.
>>
>>55928914
> __init__
This language is such a mess.
>>
>>55933251

> 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.
Thread posts: 84
Thread images: 6


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.