0

In python if i wrote

g='u'
g.upper()

The output is U. My question is: When I write the code g.upper() ,what variable a pass to the function? Secondly why (g.upper()) it works? Because I learned that the dot is use to recall the class methods,so upper() is definitly not a class method of g which is a string variable.

How ever ,How can I write a code like upper() that can be apply on integer and on out I get Interger incremented or decremente a fixed amount? I tried to do like :

def ad(a, b):
    print('Sum::--', a + 8)


#ad(5, 8)
v=5
v.ad()

Please give me knowledge,because I didn't find anything on web about this things .Thanks.

leeon
  • 9
  • 2
  • Why do you think `upper` is not a class method of `str` (the class of strings)? Run `help(str)` and see what it says. Also check `'u'.__class__`. – Charles Duffy Oct 15 '22 at 19:58
  • And no, Python is not Ruby where you can modify built-in classes to add more methods, thank ${deity}. – Charles Duffy Oct 15 '22 at 19:59
  • 1
    Here is a link to the `str` type [documentation](https://docs.python.org/3/library/stdtypes.html#str) which might help clarify the`str` type class, and show its methods. – S3DEV Oct 15 '22 at 20:03
  • I said upper is not a method of g!Because for me g is a string type variable or object. – leeon Oct 15 '22 at 20:04
  • *Because for me `g` is a string type variable or object* -- why does this mean it can't have methods? – Charles Duffy Oct 15 '22 at 20:06
  • For my understanding g (which is a string ) is not a class defined by me.Any I don't want to argue. – leeon Oct 15 '22 at 20:09
  • My question arise from::-- request.next_chunk() where request = build(API_SERVICE_NAME, API_VERSION, credentials=credentials).videos().insert( part=','.join(body.keys()), body=body, media_body=MediaFileUpload(videoPath, chunksize=-1, resumable=True) ) The next_chunk() is a method of class MediaIoBaseDownload(object) and not MediaFileUpload nor of the build(API_SERVICE_NAME, API_VERSION, credentials=credentials). Then How can it works?I got the code fromhttps://github.com/SteBurz/youtube-uploader ,Please help to understand ,I'm here to learn.Thanks. – leeon Oct 15 '22 at 20:26
  • "g" is not a class defined by you, no. `'u'` is a string literal, which will be parsed into a `str` instance object and assigned to `g`. `g` now holds a `str` object, which has all the methods of the builtin `str` class. You don't do `g = str(...)` (), because writing `'...'` is basically a special case in the Python syntax which will do `str(...)` behind the scenes. Because, what would you write inside `...` to specify the string contents anyway? There's *gotta* be some special handling for these primitive types. – deceze Oct 17 '22 at 09:23

0 Answers0